The scenario for generating a unique ID is very common, such as temporary cache file names, temporary variables, temporary security codes, and so on, and the uniqid () function generates a unique ID based on the current time in microseconds. Because the generation of unique IDs is associated with microsecond time, the uniqueness of the IDs is very reliable.
Generated unique ID The string returned by default has 13 strings long, if you do not define a prefix for a unique ID, you can return up to 23 string lengths, and if you combine the MD5 () function, the resulting unique ID will be more reliable, and this generated ID has the greatest advantage over the randomness of an ID that can be sorted. In particular, some values that need to be stored in the database.
One, function prototypes
String Uniqid ([string prefix [, BOOL more_entropy]])
The prefix and length of a unique ID can be defined
Two, version compatible
PHP 3, PHP 4, PHP 5
Third, function base usage and example
1, generate a unique ID
2, combine the MD5 () function to generate a unique ID
Output: dfbc5c8c6438de075da28b3c8a413fd0
3, generate multiple unique IDs, as measured in microseconds
Output:
4bfd0e375396b
4bfd0e3753981
4bfd0e3753983
By the result of the build, the unique IDs are sortable.
Using the Uniqid () function to generate a unique ID can be used to generate a temporary ID as well as to generate a permanent unique ID (storage database).
Ps:php several workarounds for generating unique IDs
The following small series to everyone to collate three kinds of solutions, the specific content as follows:
1, MD5 (Time (). Mt_rand (1,1000000));
This method has a certain probability that there will be repetition
2. PHP built-in function uniqid ()
The Uniqid () function generates a unique ID based on the current time in microseconds.
W3school Reference Manual There is a sentence: "Due to system time, the ID generated by this function is not optimal." To generate an absolutely unique ID, use the MD5 () function.
The following method returns the result similar to: 5ddb650f-4389-f4a9-a100-501ef1348872
function uuid () { if (function_exists (' Com_create_guid ')) { return com_create_guid (); } else { Mt_sra nd (double) microtime () * 10000); Optional for PHP 4.2.0 and up. The random number sowing, 4.2.0 not need after. $charid = Strtoupper (MD5 (Uniqid (rand (), true)), and//generates a unique ID based on the current time (in microseconds). $hyphen = Chr (45); "-" $uuid = ".//CHR (123)//" {"substr ($charid, 0, 8). $hyphen. SUBSTR ($charid, 8, 4). $hyphen. SUBSTR ($charid, 12, 4). $hyphen. SUBSTR ($charid, 16, 4). $hyphen. substr ($charid, N.); . chr;//"}" return $uuid; }}
Com_create_guid () is a PHP self-generated unique ID method, which seems to have gone php5.
3, the official uniqid () reference manual has the user-provided method, the result is similar: {E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}
Public Function create_guid ($namespace = ") { static $guid = '; $uid = Uniqid ("", true); $data = $namespace; $data. = $_server[' request_time '); $data. = $_server[' http_user_agent '); $data. = $_server[' local_addr '); $data. = $_server[' Local_port '); $data. = $_server[' remote_addr '); $data. = $_server[' Remote_port '); $hash = Strtoupper (hash (' ripemd128 ', $uid. $guid. MD5 ($DATA))); $guid = ' {'. substr ($hash, 0, 8). ' -' . SUBSTR ($hash, 8, 4). ' -' . SUBSTR ($hash, 4). ' -' . SUBSTR ($hash, 4). ' -' . SUBSTR ($hash,.). '} '; return $guid; }