PHPRO.ORG

So, you need a totally unique ID. A Globally unique ID for your system. This function creates better randomness than the PHP mt_rand() function buy using the openssl_random_pseudo_bytes() function. The resulting GUID is based on the IETF rfc.

Function


<?php

/**
 *
 * Generate 128 bits of random data
 * @see http://tools.ietf.org/html/rfc4122#section-4.4
 * @return    string
 *
 */
function guidv4()
{
    
$data openssl_random_pseudo_bytes16 );
    
$data[6] = chrord$data[6] ) & 0x0f 0x40 ); // set version to 0100
    
$data[8] = chrord$data[8] ) & 0x3f 0x80 ); // set bits 6-7 to 10

    
return vsprintf'%s%s-%s-%s-%s-%s%s%s'str_splitbin2hex$data ), ) );
}

echo 
guidv4()."\n";
?>

Example output

ffd38d82-741b-4562-8f7f-06b1992c0025