note 69164 deleted from function.uniqid by googleguy
| From: | googleguy@php.net | Date: | Tue, 15 Jan 2013 05:08:24 +0000 |
| Subject: | note 69164 deleted from function.uniqid by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193062@lists.php.net to get a copy of this message | ||
Note Submitter: mimec
----
Here is the correct version of a function generating a pseudo-random UUID according to RFC 4122:
<?php
function uuid()
{
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
}
?>
The version and variant is located at the MSB (most significant bits) of the time_hi_and_version and
clock_seq_hi_and_reserved fields, not the LSB as in dholmes version.