UUID (Universally Unique Identifier), GUID hopes to generate a unique identification code within the entire time and space range, which is necessary in a distributed computing environment. However, if you just want to generate a "local unique identification code" in a limited local environment, using UUID is to kill a chicken and use a knives. This "local unique identification code" is called LUID (Local Unique Identifier)
For example, when I was developing a website program using php, in order to avoid the user opening the same web page multiple times at the same time, I hope that the session name conflict will be conflicted by the user, but $_SESSION['param'], but $_SESSION[$luid]['param'], and then pass the $luid value in other ways to ensure that the 'param' parameter is not overwritten. I looked up other people's solutions, and the UUID generated algorithm is spreading hundreds of lines. I considered that because it is in the SESSION space, it is a limited environment, and its uniqueness does not have to be too high, as long as it is unique within the lifetime of the same SESSION, so I have the following code:
/**
* Returns a unique string 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());
According to the principle, the string returned by microtime() is already unique. I tested that even if microtime() is executed continuously, the return value is more than 100us. The interval between users clicking and passing on the network and processing by the server is more than tens of ms. Adding md5 just makes the result messy.
For example, when I was developing a website program using php, in order to avoid the user opening the same web page multiple times at the same time, I hope that the session name conflict will be conflicted by the user, but $_SESSION['param'], but $_SESSION[$luid]['param'], and then pass the $luid value in other ways to ensure that the 'param' parameter is not overwritten. I looked up other people's solutions, and the UUID generated algorithm is spreading hundreds of lines. I considered that because it is in the SESSION space, it is a limited environment, and its uniqueness does not have to be too high, as long as it is unique within the lifetime of the same SESSION, so I have the following code:
Copy the codeThe code is as follows:
/**
* Returns a unique string 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());
According to the principle, the string returned by microtime() is already unique. I tested that even if microtime() is executed continuously, the return value is more than 100us. The interval between users clicking and passing on the network and processing by the server is more than tens of ms. Adding md5 just makes the result messy.