TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   need advice for creating personalized urls (http://www.talkphp.com/general/2330-need-advice-creating-personalized-urls.html)

webosb 02-25-2008 02:44 PM

need advice for creating personalized urls
 
i'm trying to figure out whats the best way I can do this..

i want to create a unique string either based on userid or email for personalized urls sort of like how youtube is:
Code:

http://youtube.com/watch?v=eBGIQ7ZuuiU
-- does anyone know which approach I should take on this?

Village Idiot 02-25-2008 02:52 PM

You just pull the row out with that value from the database. So that youtube video has an ID (or other identifying entity) of eBGIQ7ZuuiU

abiko 02-25-2008 03:04 PM

You can always md5() or sha1() the string combined with the time of creation.
Here is a SHA1 solution - it generates a 8 char string.
strtoupper is there if you want to have uppercase letters instead lowercase.
PHP Code:

<?php
// MD5 generates a 32-bit
// If you want to use 8bit or 
$session_id sha1(uniqid(mt_rand(), true));
$id substr$session_id08);
$id strtoupper$id );
echo 
$id;
?>

Now you have an identifier for you so you just save it in the table and you're set to go.
if you url is
Code:

http://yoursite.com/watch/C21D36C7/
Your script searches for the ident string (C21D36C7) for the item - your every item has an unique string generated with the snippet above.

So in your database table should look like this
|id | ident |....other | fields

Hope this helpes

Wildhoney 02-25-2008 03:27 PM

I've just wrote us a nice function which generates identifiers just like YouTube :-) ! Not just limited to the hexadecimal range of 0 to F.

php Code:
function getIdentifier($iLength, $bUseUpper = true, $bUseLower = true, $bUseNumber = true)
{
    $szId = '';
    $aChars = array();
   
    if($bUseUpper)  $aChars[] = range(65, 90);
    if($bUseLower)  $aChars[] = range(97, 122);
    if($bUseNumber) $aChars[] = range(48, 57);
   
    $iCount = count($aChars);
   
    if($iCount == 0)
    {
        return null;
    }
   
    for($iIndex = 0; $iIndex < $iLength; $iIndex++)
    {
        $iChannel = rand(0, $iCount - 1);
        $szId .= chr($aChars[$iChannel][rand(0, count($aChars[$iChannel]) - 1)]);
    }
   
    return $szId;
}

echo 'ID = ' . getIdentifier(6);

You can even specify which types to include using the 3 arguments after the identifier length. All of which are set to true by default. You'll of course want to add your own checker to see if the returned identifier is unique. I recommend sticking in a do while loop for that -- outside of the function itself.

webosb 02-25-2008 04:14 PM

Quote:

Originally Posted by Wildhoney (Post 11398)
I've just wrote us a nice function which generates identifiers just like YouTube :-) ! Not just limited to the hexadecimal range of 0 to F.

php Code:
function getIdentifier($iLength, $bUseUpper = true, $bUseLower = true, $bUseNumber = true)
{
    $szId = '';
    $aChars = array();
   
    if($bUseUpper)  $aChars[] = range(65, 90);
    if($bUseLower)  $aChars[] = range(97, 122);
    if($bUseNumber) $aChars[] = range(48, 57);
   
    $iCount = count($aChars);
   
    if($iCount == 0)
    {
        return null;
    }
   
    for($iIndex = 0; $iIndex < $iLength; $iIndex++)
    {
        $iChannel = rand(0, $iCount - 1);
        $szId .= chr($aChars[$iChannel][rand(0, count($aChars[$iChannel]) - 1)]);
    }
   
    return $szId;
}

echo 'ID = ' . getIdentifier(6);

You can even specify which types to include using the 3 arguments after the identifier length. All of which are set to true by default. You'll of course want to add your own checker to see if the returned identifier is unique. I recommend sticking in a do while loop for that -- outside of the function itself.

so correct me if i'm wrong, it should work something like this:

(im just using dupecheckid() function as an example - but the function would run a query like SELECT * FROM table WHERE uid = $id)

$id = getIdentifier(6);
if(dupecheckid($id)){
//identical id found in database
$id = getIdentifier(6);
}else{
//continue doing what it is doing
}

Wildhoney 02-25-2008 04:23 PM

php Code:
do   $iId = getIdentifier(6);
while   (!dupecheckid($iId));

var_dump($iId);


All times are GMT. The time now is 12:50 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0