TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-25-2008, 02:44 PM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default 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?
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 02-25-2008, 02:52 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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
__________________

Village Idiot is offline  
Reply With Quote
Old 02-25-2008, 03:04 PM   #3 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Big Grin

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
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 02-25-2008, 03:27 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
Alan @ CIT (02-26-2008), webosb (02-25-2008)
Old 02-25-2008, 04:14 PM   #5 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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
}
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 02-25-2008, 04:23 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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

var_dump($iId);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
webosb (02-25-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design