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:

Anonymous said...

10 points

அசுரன் திராவிடன் said...

it is very userful.great job thanks dude

Web Development Irvine said...

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

Anonymous said...

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

Christopher Roy said...

@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.

Anonymous said...

just excellent

Anonymous said...

Hi,

I just found this post & it was very useful.

Thanks Christopher :)

j0s said...

Thanks a Lot. Started Liking PHP more .

Jeffery said...

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

Adam K. said...

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

Conrad said...

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

Marco said...

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);

Anonymous said...

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

Anonymous said...

Great! thank you very much! :)

gagan david said...

thanks nice post

Iain said...
This comment has been removed by the author.
Iain said...

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)

Anonymous said...

very useful, thanks

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Anonymous said...

Standing ovation for your awesomeness...
Thank you

Rohann said...

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

Anonymous said...

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.

Anonymous said...

Thanks it helped...

majormukoro said...
This comment has been removed by the author.
majormukoro said...

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

Anonymous said...

it's awesome

Anonymous said...

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

Anonymous said...

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

Unknown said...

Very helpful. Thanks

Unknown said...

Much thanks for this tuto

Anonymous said...

very helpful..thanks a lot!

Anonymous said...

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

Anonymous said...

This page is awesome!! THANK YOU!

Anonymous said...

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