Small series on the internet to check a lot of PHP to generate a unique ID method of the article, found that there are many methods, special finishing this article with you to share PHP to generate a unique ID solution, I hope you like.
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.
The W3school reference manual says: "The IDs generated through this function are not optimal because of the system time." If you want to generate an absolutely unique ID, use the MD5 () function.
The following method returns a similar result: 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. Sow the seeds casually, 4.2.0 do not need them later.
$charid = Strtoupper (MD5 (Uniqid (rand (), True)),//////////////////////////////
$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, a);
. chr ()//"}" return
$uuid;
}
}
Com_create_guid () is a PHP-generated unique ID method, after php5 seems to have gone.
3, the official uniqid () the reference manual has the user to provide the 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, a).
'} ';
return $guid;
}
The above is PHP to generate a unique ID of three kinds of programs, I hope to help you learn.