Method One: PHP built-in function uniqid () the uniqid () function generates a unique ID based on the current time in microseconds. Grammar
Uniqid (prefix,more_entropy)
Parameters |
Description |
Prefix |
Optional. Specify a prefix for the ID. This parameter is useful if two scripts generate IDs exactly in the same microsecond. |
More_entropy |
Optional. Specifies more entropy at the end of the return value. |
Description
If the prefix parameter is empty, the returned string has 13 string lengths. If the more_entropy parameter is set to True, it is 23 strings long.
If the more_entropy parameter is set to True, additional entropy is added at the end of the return value (using a combination line with the remainder generator), which results in better uniqueness.
return value
Returns a unique identifier in the form of a string.
Hints and Notes
Note: The ID generated by this function is not optimal due to system time. To generate an absolutely unique ID, use the MD5 () function (find in the string function Reference).
Example
<?phpecho uniqid ();? >
The output is similar to:
4415297e3af8c
Method Two: PHP built-in function Com_create_guid () The COM_CREATE_GUID function generates a globally unique identifier that resembles the following: {E787d62d-4b4d-492c-9301-6ffd53a1f2d7} Such a string, the trim function or other methods can be used to handle the curly brace, namely: Trim (Com_create_guid (), ' {} ');
<?phpfunction GUID () { if (function_exists (' com_create_guid ') = = = True) { return trim (com_create_guid (), '{}'); } return sprintf ('%04x%04x-%04x-%04x-%04x-%04x%04x%04x ', Mt_rand (0, 65535), Mt_rand (0, 65535), Mt_rand (0, 65535), Mt_ Rand (16384, 20479), Mt_rand (32768, 49151), Mt_rand (0, 65535), Mt_rand (0, 65535), Mt_rand (0, 65535));}? >
Method three: Using database functions such as PHP to connect to MySQL, you can use mysql_query ("Select UUID () as UID"), to derive a unique ID, You can also draw a unique string similar to: 82685c7c-f2a6-1032-b525-fe3407868e36
Methods: Other custom strings or combinations of functions, such as using microseconds or timestamps plus random numbers, generate a unique ID similar to: MD5 (Time (). Mt_rand (1,1000000));
Method of generating standard unique UUID for PHP