PHP uniqid () function
The uniqid () function generates a unique ID based on the current time in microseconds.
Note:Because the system time is used, the IDS generated by this function are not optimal. To generate an absolutely unique ID, use the MD5 () function (please refer to the string function reference ).
<? PHP
Echo uniqid ();
?>
In this example, a 32-character unique string is generated.
<? PHP
$ Token = MD5 (uniqid (RAND ()));
Echo $ token;
?>
Uniqid () returns a unique identifier with a prefix based on the current time accurate to microseconds.
It is based on the current time, but it does not explain the relationship with the current time.
Echo uniqid (); we can see that uniqid is always a hexadecimal number with a continuously changing length of 13.
<? PHP
Echo hexdec (uniqid ()/(Time () + microtime ());
?>
The output is about 1048576.
It can be concluded that uniqid is obtained after the current time is accurate to microsecond and then multiplied by 1048576 (20 power of 2) and finally converted to hexadecimal.
Uniqid can be used more widely after you know the relationship between uniqid and time. For example, uniqid can be used as the file name of a post in a text forum.
In the post index, you can easily search for a post by time.
Integration up and downCodeLook, I think his role is to generate a 32-bit non-repeating character.
The uniqid () function is based on the current time in microseconds. Therefore, in the case of high concurrency, repetition may occur, the solution is to generate a random number under this premise, and then combine the two to generate a new number, which will reduce the probability of repetition. If you still want to be more accurate, you can add the MD5 code of the client's IP address to generate the Code together. In this way, the probability of duplication is very low. It can be said that there will be almost no duplication.
<? PHP
Function getrand (){
Return uniqid (). Rand (1, 100000 );
}
Echo getrand ();
Exit;
?>