Php method for generating a standard unique UUID

Source: Internet
Author: User
Tags 04x

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.