This example describes the use of the Uniqid () function in PHP. Share to everyone for your reference. The specific methods are analyzed as follows:
The Uniqid () function generates a unique ID based on the current time in microseconds.
Note: The IDs generated through this function are not optimal because of the system-based time. To generate an absolutely unique ID, use the MD5 () function (find in the string function Reference).
Copy Code code as follows:
This example produces a unique string of 32 characters.
Copy Code code as follows:
<?php
$token = MD5 (Uniqid (rand ());
Echo $token;
?>
Uniqid () returns a unique identifier with a prefix that is accurate to microseconds based on the current time.
It's just that it's based on the current time, but it doesn't explain how it relates to the current time.
Echo uniqid (); You can see that uniqid is always a changing hexadecimal number with a constant length of 13.
Take a look at the following code:
Copy Code code as follows:
<?php
Echo Hexdec (Uniqid ())/(Time () +microtime ());
?>
The output is basically around 1048576.
It can be concluded that the uniqid is the current time accurate to microseconds and then multiplied by 1048576 (2 of the 20 power) finally converted to hexadecimal obtained.
Know Uniqid and time relationship after Uniqid can have a wider use, such as in the Forum can use Uniqid as a post file name.
In the post index, you can easily find posts by the time.
In combination with the upper and lower codes, I think his role is to generate a 32-bit character without repetition.
The Uniqid () function itself is based on the current time in microseconds, so in the case of high concurrency there will certainly be duplication, the solution is that you can regenerate in this premise into a random number, and then the combination of two produced a new number, which will reduce the probability of repetition. If you still want more accurate words can also add the client's IP Md5 code to generate together, so the probability of repetition should be extremely low, it can be said that almost will not repeat.
Copy Code code as follows:
<?php
function Getrand () {
Return Uniqid (). Rand (1, 100000);
}
Echo Getrand ();
Exit
?>
I hope this article will help you with your PHP programming.