View Single Post
Old 02-25-2008, 04:14 PM   #5 (permalink)
webosb
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