Home > Backend Development > PHP Tutorial > PHP uniqid() function usage, phpuniqid function usage_PHP tutorial

PHP uniqid() function usage, phpuniqid function usage_PHP tutorial

WBOY
Release: 2016-07-13 10:15:29
Original
1516 people have browsed it

PHP uniqid() function usage, phpuniqid function usage

The example in this article describes the usage of uniqid() function in PHP. Share it with everyone for your reference. The specific method analysis is as follows:

The uniqid() function generates a unique ID based on the current time in microseconds.
Note: The ID generated by this function is suboptimal because it is based on system time. To generate an absolutely unique ID, use the md5() function (find it in the String Functions Reference).

Copy code The code is as follows:
echo uniqid();
?>

This example produces a unique string of 32 characters.

Copy code The code is as follows:
$token = md5(uniqid(rand()));
echo $token;
?>


uniqid() returns a prefixed unique identifier based on the current time accurate to microseconds.
It just says that it is based on the current time, but it does not explain how it is related to the current time.
echo uniqid(); You can see that uniqid is always a changing hexadecimal number with a length of 13.

Let’s take a look at the following code:

Copy code The code is as follows:
echo hexdec(uniqid())/(time()+microtime());
?>

The output is basically around 1048576.

It can be concluded that uniqid is the current time accurate to microseconds, multiplied by 1048576 (2 to the 20th power) and finally converted to hexadecimal.
After knowing the relationship between uniqid and time, uniqid can have a wider range of uses. For example, uniqid can be used as the file name of a post in a forum.

In the post index, you can easily search for posts by time.

Looking at the above and below codes, I think its function is to generate a non-repeating 32-bit character

The uniqid() function itself is based on the current time in microseconds, so duplication will definitely occur in high concurrency situations. The solution is that you can generate a random number under this premise, and then The combination of the two produces a new number, which will reduce the probability of repetition. If you still want to be more precise, you can also add the MD5 code of the client's IP to generate it together. In this way, the probability of duplication is extremely low, and it can be said that there will be almost no duplication.

Copy code The code is as follows:
function getRand(){
Return uniqid() . rand(1, 100000);
}
echo getRand();
exit;
?>

I hope this article will be helpful to everyone’s PHP programming design.

Please help me explain the uniqid() function in php; I found it in English in the php manual

uniqid
produces a value of only one.
Syntax: string uniqid(string prefix);
Return value: String
Function type: Encoding processing
Content description
This function will be based on the current millisecond and the specified prefix string Produces a unique string. The parameter prefix is ​​the preceding string, which can be up to 114 characters.

echo uniqid(); You can see that uniqid is always a changing hexadecimal number with a length of 13.
echo hexdec(uniqid())/(time()+microtime());
?>
The output is basically around 1048576.
It can be concluded that uniqid is the current time accurate to microseconds, multiplied by 1048576 (2 to the 20th power) and finally converted to hexadecimal.
After knowing the relationship between uniqid and time, uniqid can be used for a wider range of purposes. For example, in a text forum, uniqid can be used as the file name of a post.
In the post index, you can easily search for posts by time.
Reference: hi.baidu.com/...7.html

I am learning PHP and asked under what circumstances should uniqid() be used?

846189340160532f6c51bf26ab596f55

It will be used when detecting security horses

?>

It is also used when making random numbers

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904920.htmlTechArticlePHP uniqid() function usage, phpuniqid function usage This article explains the usage of uniqid() function in PHP. Share it with everyone for your reference. The specific method is analyzed as follows: uniqid() function base...
Related labels:
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