note 69312 deleted from function.uniqid by betz
| From: | betz@php.net | Date: | Thu, 31 Aug 2006 17:54:05 +0000 |
| Subject: | note 69312 deleted from function.uniqid by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-116555@lists.php.net to get a copy of this message | ||
Note Submitter: DragonSpirit254
----
I created this function to generate an extremely hard-to-predict Uniq ID (32-Byte HEX) without the
worry of collisions (as with the MD5 function.)
<?php
function uuid() {
$uuid_num = str_replace("
","",str_replace("0.","",microtime()));
$uuid_num2=explode(":",chunk_split($uuid_num,4,":"));
for($uuid_i=0;$uuid_i<count($uuid_num2);$uuid_i++)
$uuid_out.=dechex(rand(64,255).$uuid_num2[$uuid_i]);
while(strlen($uuid_out)<32) { $uuid_out.=dechex(rand(1,16)); }
return $uuid_out;
}
?>
The only way for a collision to happen with this function is if the function was executed at the
exact microsecond AND all 20+ randomly generated numbers matched.
Any thoughts, ideas, fixes, improvements?
- Steve