PHP uniqid () Functional usage _php Tips

Source: Internet
Author: User
Tags php programming rand repetition unique id

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:
<?php
Echo Uniqid ();
?>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.