How to generate a unique id by php
This article is about how php generates a unique id. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Md5 (time (). Mt_rand (110000000)
There is a certain probability that this method will be repeated.
2. Php built-in function uniqid ()
The uniqid () function generates a unique ID based on the current time in microseconds.
There is a sentence in the w3school reference manual: "the ID generated by this function is not optimal because it is based on system time. 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_srand ((double) microtime () * 10000); / / optional for php 4.2.0 and up. Feel free to sow any number of seeds. You won't need them after 4.2.0. $charid = strtoupper (md5 (uniqid (rand (), true); / / generate a unique id based on the current time (millisecond). $hyphen = chr (45); / / "-" $uuid =''. / / chr / / "{" substr ($charid, 0,8). $hyphen. Substr ($charid, 8,4). $hyphen. Substr ($charid, 12,4). $hyphen. Substr ($charid, 16,4). $hyphen. Substr ($charid, 20,12); / / .chr (125); / / "}" return $uuid;}}
Com_create_guid () is the only id method that comes with php, which seems to be gone after php5.
3. The official uniqid () reference manual has a method provided by the user, and 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, 12,4). '-'. Substr ($hash, 16,4). '-'. Substr ($hash, 20,12). '}'; return $guid;} Thank you for reading! This is the end of this article on "how php generates a unique id". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!