<?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>";
?>
36 comments:
10 points
it is very userful.great job thanks dude
Thank you so much for the example. Still, I still have to convert your script into something I really need. Thanks anyway.
Will the generated Guid-style number always be "unique", no matter how many I generate?
@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.
just excellent
Hi,
I just found this post & it was very useful.
Thanks Christopher :)
Thanks a Lot. Started Liking PHP more .
Great Job......
Its Simple and neat :) Thanks a lot :)
Thanks, this helped a lot for a website I'm working on!
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
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);
i don't leave comments very often, but this was actually a useful post. Thank you.
Great! thank you very much! :)
thanks nice post
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)
very useful, thanks
Thanks so much. P THE GOOD KEEP UP THE GOOD JOB.
nice - thx! is it possible to impove execution time? i get 0.05/6 on other functions yours is 1.x-9.x.
md5 doesn't encrypt - it hashes.........
Standing ovation for your awesomeness...
Thank you
Extremely useful. After hours searching the web, I got exactly what I needed on your website. Thank you.
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.
Thanks it helped...
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
it's awesome
Thank you very much. You saved my day. Literally.
how to generate only numeric unique id??????????
Very helpful. Thanks
Much thanks for this tuto
very helpful..thanks a lot!
3rd type of generating unique id is amazing !!!!!!!!!
This page is awesome!! THANK YOU!
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
Post a Comment