Home > Backend Development > PHP Tutorial > PHP code to generate local unique identification code LUID_PHP tutorial

PHP code to generate local unique identification code LUID_PHP tutorial

WBOY
Release: 2016-07-21 15:15:48
Original
981 people have browsed it

UUID (Universally Unique Identifier) ​​and GUID hope to generate unique identification codes throughout the entire space and time range, which is necessary in a distributed computing environment. However, if you only want to generate a unique identification code in a restricted local environment, A "local unique identifier", using UUID is just overkill. This "local unique identifier", I call it LUID (Local Unique Identifier) ​​

For example, when I use PHP to develop website programs, In order to avoid session name conflicts caused by users opening the same web page multiple times at the same time, we hope that the saved session is not $_SESSION['param'], but $_SESSION[$luid]['param'], and then pass $ through other methods. luid value to ensure that the 'param' parameter is not overwritten. After searching for other people's solutions, they all generate UUID, and the algorithm for generating UUID is filled with hundreds of lines. I considered that because it is in the SESSION space, it is a restricted The uniqueness of the environment does not need to be too high, as long as it is unique within the lifetime of the same SESSION, so the following code is available:

Copy code The code is as follows:

/**
* Returns a string that is unique in the local system.
* returns a 32-character string, such as '7dac352074f221f3edc74d265c65a636', or 'd198d8fc56ffed627f3f8313d6f06acf'
*/
function LUID(){
return MD5(microtime());
}


In fact, it’s just one line. return MD5(microtime());

Logically speaking, the string returned by microtime() is already unique. I tested it. Even if microtime() is executed continuously, the return value will have a difference of more than 100us, and the interval between the user's click, transmission on the network, and processing by the server is far more than tens of milliseconds. Adding md5 only makes the results cluttered.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326068.htmlTechArticleUUID (Universally Unique Identifier) ​​and GUID hope to generate unique identification codes throughout the entire space and time range. This is It is necessary in a distributed computing environment. However, if you just want to do it in a restricted...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template