<?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 (about, true);
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>";
?>
//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 (about, true);
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>";
?>
//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>";
?>
//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>";
?>
// 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>";
?>
10 points
ReplyDeleteit is very userful.great job thanks dude
ReplyDeleteThank you so much for the example. Still, I still have to convert your script into something I really need. Thanks anyway.
ReplyDeleteWill the generated Guid-style number always be "unique", no matter how many I generate?
ReplyDelete@anon
ReplyDeleteGUIDS 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.
just excellent
ReplyDeleteHi,
ReplyDeleteI just found this post & it was very useful.
Thanks Christopher :)
Thanks a Lot. Started Liking PHP more .
ReplyDeleteGreat Job......
ReplyDeleteIts Simple and neat :) Thanks a lot :)
Thanks, this helped a lot for a website I'm working on!
ReplyDeleteWell done
ReplyDeleteAdded you here
http://digg.com/news/technology/corner_four_ways_to_generate_unique_id_by_php
and a tweet
https://twitter.com/@webguy_ca
very useful...thank you!
ReplyDeleteI use this for create id for scheduled script:
date('YmdHis').'_'.str_pad(rand(1,999999), 6, '0', STR_PAD_LEFT);
i don't leave comments very often, but this was actually a useful post. Thank you.
ReplyDeleteGreat! thank you very much! :)
ReplyDeletethanks nice post
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWith option number two, wouldn't it better to use:
ReplyDeletedate("YmdHis") instead of date("Ymdhis")
(little h is 12 hour, upper H is 24 hour)
very useful, thanks
ReplyDeleteThanks so much. P THE GOOD KEEP UP THE GOOD JOB.
ReplyDeletenice - thx! is it possible to impove execution time? i get 0.05/6 on other functions yours is 1.x-9.x.
ReplyDeletemd5 doesn't encrypt - it hashes.........
ReplyDeleteStanding ovation for your awesomeness...
ReplyDeleteThank you
Extremely useful. After hours searching the web, I got exactly what I needed on your website. Thank you.
ReplyDeleteJust 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.
ReplyDeleteThanks it helped...
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for your tutorial. It really help a lot.
ReplyDeleteI 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
it's awesome
ReplyDeleteThank you very much. You saved my day. Literally.
ReplyDeletehow to generate only numeric unique id??????????
ReplyDeleteVery helpful. Thanks
ReplyDeleteMuch thanks for this tuto
ReplyDeletevery helpful..thanks a lot!
ReplyDelete3rd type of generating unique id is amazing !!!!!!!!!
ReplyDeleteThis page is awesome!! THANK YOU!
ReplyDeletethis is very helpful to my project. i can modify used this code for custom id.
ReplyDelete3rd type is super.
!!!!!! THANK YOU !!!!!!
ONE TYPE
";
?>
ANOTHER TYPE