PHP generates a local unique identifier LUID code _php instance

Source: Internet
Author: User
Tags md5 uuid
UUID (Universally unique Identifier), the GUID is expected to generate a unique identifier within the entire space-time range, which is necessary in a distributed computing environment. However, if you simply want to generate a "local unique identifier" in a limited local environment, using the UUID is overkill, the "local unique identifier", which I call LUID (the locals unique Identifier)

For example, when I use PHP to develop a Web site program, in order to avoid users at the same time open the same Web page to cause the session name conflict problem, the hope is to save the session is not $_session[' param '], but $_session[$luid [' param '], The $luid value is passed in other ways to ensure that the ' param ' parameter is not overwritten. Looking for someone else's solution, is to generate a UUID, and the algorithm to generate a UUID hundreds of lines. I consider that because it is in the session space, is a limited environment, the uniqueness of the strength does not have to be too high, as long as the same session of the lifetime of the only can, so there is the following code:

Copy Code code as follows:

/**
* Returns a unique string that is unique in the local system.
* Returns a 32 character string, in the form of ' 7dac352074f221f3edc74d265c65a636 ', or ' D198D8FC56FFED627F3F8313D6F06ACF '
*/
function Luid () {
Return MD5 (Microtime ());
}


Actually on one line. Return MD5 (Microtime ());

The string returned by reason Microtime () is already unique, I measured, even if the continuous execution of microtime (), the return value has a difference of more than 100US, and the user clicks, and on the network transfer again by the server processing interval far more than dozens of Ms. Plus MD5 just makes the results messy.

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.