You are here

function _uuid_generate_php in Universally Unique IDentifier 7

Generates a UUID v4 (RFC 4122 section 4.4) using PHP code.

The UUID layout and fields are defined in section 4.1.2.

Note that there are inconsistencies in the RFC with respect to bit numbering. Network Order is correct, so the most significant bit always appears first (in left-to-right sequence). See errata 3546: http://www.rfc-editor.org/errata_search.php?rfc=4122&eid=3546

Based on code from http://php.net/uniqid

See also

http://www.rfc-editor.org/rfc/rfc4122.txt

1 string reference to '_uuid_generate_php'
uuid_generate in ./uuid.inc
Generates an universally unique identifier.

File

./uuid.inc, line 236
Handling of universally unique identifiers.

Code

function _uuid_generate_php() {

  // We limit each generated number to 16 bits (maximum value 0xffff)
  // because mt_rand() returns a *signed* integer, and hence a 32-bit
  // value can only have a 31-bit magnitude. Constructing a 32-bit
  // number from two 16-bit random numbers guarantees that all 32 bits
  // are random.
  return sprintf('%04x%04x-%04x-4%03x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xfff), mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
}