Saturday, August 04, 2007

Four ways to generate unique id by PHP

1. Using uniqid() function

<?php

//creates a unique id with the 'about' prefix
$a uniqid(about);
echo 
$a;
echo 
"<br>";

//creates a longer unique id with the 'about' prefix
$b uniqid (abouttrue);
echo 
$b;
echo 
"<br>";

//creates a unique ID with a random number as a prefix - more secure than a static prefix 
$c uniqid (rand(), true);
echo 
$c;
echo 
"<br>";

//this md5 encrypts the username from above, so its ready to be stored in your database
$md5c md5($c);
echo 
$md5c;
echo 
"<br>";

?>

2. Using current time + IP style

<?php

//You can also use $stamp = strtotime ("now"); But I think date("Ymdhis") is easier to understand.
$stamp date("Ymdhis");
$ip $_SERVER['REMOTE_ADDR'];
$orderid "$stamp-$ip";
$orderid str_replace(".""""$orderid");
echo(
$orderid);
echo 
"<br>";

?>

3. Generate custom length unique id

<?php

//set the random id length 
$random_id_length 10

//generate a random id encrypt it and store it in $rnd_id 
$rnd_id crypt(uniqid(rand(),1)); 

//to remove any slashes that might have come 
$rnd_id strip_tags(stripslashes($rnd_id)); 

//Removing any . or / and reversing the string 
$rnd_id str_replace(".","",$rnd_id); 
$rnd_id strrev(str_replace("/","",$rnd_id)); 

//finally I take the first 10 characters from the $rnd_id 
$rnd_id substr($rnd_id,0,$random_id_length); 

echo 
"Random Id: $rnd_id" ;
echo 
"<br>";

?>

4. Generate XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX style unique id, (8 letters)-(4 letters)-(4 letters)-(4 letters)-(12 letters)

<?php

// Generate Guid 
function NewGuid() { 
    
$s strtoupper(md5(uniqid(rand(),true))); 
    
$guidText 
        
substr($s,0,8) . '-' 
        
substr($s,8,4) . '-' 
        
substr($s,12,4). '-' 
        
substr($s,16,4). '-' 
        
substr($s,20); 
    return 
$guidText;
}
// End Generate Guid 

$Guid NewGuid();
echo 
$Guid;
echo 
"<br>";

?>

36 comments:

  1. Anonymous2:28 AM

    10 points

    ReplyDelete
  2. Thank you so much for the example. Still, I still have to convert your script into something I really need. Thanks anyway.

    ReplyDelete
  3. Anonymous2:09 AM

    Will the generated Guid-style number always be "unique", no matter how many I generate?

    ReplyDelete
  4. @anon
    GUIDS are short for guaranteed unique identifier. They area also larger then the number of particles in the known universe. So if you ever get 2 that are the same; you win the interwebs.

    ReplyDelete
  5. Anonymous12:07 PM

    just excellent

    ReplyDelete
  6. Anonymous11:33 AM

    Hi,

    I just found this post & it was very useful.

    Thanks Christopher :)

    ReplyDelete
  7. Thanks a Lot. Started Liking PHP more .

    ReplyDelete
  8. Jeffery6:31 PM

    Great Job......
    Its Simple and neat :) Thanks a lot :)

    ReplyDelete
  9. Adam K.12:46 PM

    Thanks, this helped a lot for a website I'm working on!

    ReplyDelete
  10. Well done

    Added you here
    http://digg.com/news/technology/corner_four_ways_to_generate_unique_id_by_php

    and a tweet

    https://twitter.com/@webguy_ca

    ReplyDelete
  11. very useful...thank you!

    I use this for create id for scheduled script:

    date('YmdHis').'_'.str_pad(rand(1,999999), 6, '0', STR_PAD_LEFT);

    ReplyDelete
  12. Anonymous10:53 PM

    i don't leave comments very often, but this was actually a useful post. Thank you.

    ReplyDelete
  13. Anonymous5:01 PM

    Great! thank you very much! :)

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. With option number two, wouldn't it better to use:

    date("YmdHis") instead of date("Ymdhis")

    (little h is 12 hour, upper H is 24 hour)

    ReplyDelete
  16. Anonymous7:00 PM

    very useful, thanks

    ReplyDelete
  17. Anonymous2:52 AM

    Thanks so much. P THE GOOD KEEP UP THE GOOD JOB.

    ReplyDelete
  18. Anonymous7:34 AM

    nice - thx! is it possible to impove execution time? i get 0.05/6 on other functions yours is 1.x-9.x.

    ReplyDelete
  19. Anonymous9:06 PM

    md5 doesn't encrypt - it hashes.........

    ReplyDelete
  20. Anonymous10:00 PM

    Standing ovation for your awesomeness...
    Thank you

    ReplyDelete
  21. Rohann11:37 PM

    Extremely useful. After hours searching the web, I got exactly what I needed on your website. Thank you.

    ReplyDelete
  22. Anonymous5:58 AM

    Just ran into this and wanted to point out a minor clarification. The G in GUID stands for GLOBAL, not guaranteed. GUIDs can be duplicates but the chances of a collision are astronomically small. Still, it is a good idea to check that it does not already exist before putting it into your DB or what have you.

    ReplyDelete
  23. Anonymous4:35 AM

    Thanks it helped...

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete
  25. Thanks for your tutorial. It really help a lot.

    I am using this code



    //set the random id length
    $random_id_length = 10;

    //generate a random id encrypt it and store it in $rnd_id
    $rnd_id = crypt(uniqid(rand(),1));

    //to remove any slashes that might have come
    $rnd_id = strip_tags(stripslashes($rnd_id));

    //Removing any . or / and reversing the string
    $rnd_id = str_replace(".","",$rnd_id);
    $rnd_id = strrev(str_replace("/","",$rnd_id));

    //finally I take the first 10 characters from the $rnd_id
    $rnd_id = substr($rnd_id,0,$random_id_length);

    echo "Random Id: $rnd_id" ;
    echo "
    ";



    But getting this error

    Notice: crypt(): No salt parameter was specified. You must use a randomly generated salt and a strong hash function to produce a secure hash. in C:\xampp\htdocs\phplab\rand.php on line 7
    Random Id: 3sAe3fTlyn

    Please how do I create salt for this

    ReplyDelete
  26. Anonymous11:26 AM

    it's awesome

    ReplyDelete
  27. Anonymous12:28 AM

    Thank you very much. You saved my day. Literally.

    ReplyDelete
  28. Anonymous2:11 PM

    how to generate only numeric unique id??????????

    ReplyDelete
  29. Very helpful. Thanks

    ReplyDelete
  30. Much thanks for this tuto

    ReplyDelete
  31. Anonymous1:10 PM

    very helpful..thanks a lot!

    ReplyDelete
  32. Anonymous9:38 PM

    3rd type of generating unique id is amazing !!!!!!!!!

    ReplyDelete
  33. Anonymous5:17 AM

    This page is awesome!! THANK YOU!

    ReplyDelete
  34. Anonymous12:38 PM

    this is very helpful to my project. i can modify used this code for custom id.
    3rd type is super.
    !!!!!! THANK YOU !!!!!!

    ONE TYPE
    ";
    ?>

    ANOTHER TYPE

    ReplyDelete