PHP uniqid () function usage, phpuniqid function usage _php Tutorial

Source: Internet
Author: User

PHP uniqid () function usage, phpuniqid function usage


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 ID generated by this function is not optimal due to system time. To generate an absolutely unique ID, use the MD5 () function (find in the string function Reference).
Copy the Code code as follows: <?php
Echo Uniqid ();
?>
This example produces a unique string of 32 characters.

Copy the Code code as follows: <?php
$token = MD5 (Uniqid (rand ()));
Echo $token;
?>

Uniqid () returns a unique identifier with a prefix based on the current time accurate to microseconds.
Just say based on the current time, but does not indicate how the relationship with the current time.
Echo uniqid (); You can see that uniqid is always a constantly changing hexadecimal number with a length of 13.

Take a look at the following code:
Copy the 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 multiplied by 1048576 (2 of the Power of 20) and finally converted to hexadecimal.
Knowing the relationship between Uniqid and time, uniqid can be used for a wider range of purposes, such as a file name that can be used as a post in a uniqid forum.

In the post index, it is convenient to find posts by time.

In combination with the upper and lower code, I think his role is to generate a non-repeating 32-character bar

The Uniqid () function itself is based on the current time in microseconds, so in the case of high concurrency there will definitely be duplication, the solution is that you can regenerate into a random number on this premise, and then combine the two to produce a new number, which will reduce the probability of repetition. If you still want to be accurate, you can also add the client's IP Md5 code to generate together, so the probability of repetition should be very low, it can be said that almost no repetition.
Copy the Code code as follows: <?php
function Getrand () {
Return Uniqid (). Rand (1, 100000);
}
Echo Getrand ();
Exit
?>

I hope this article is helpful to everyone's PHP programming.


PHP to help me explain the uniqid () this function; I'm looking for 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 produces a unique string based on the current millisecond and the specified predecessor string. The parameter prefix is a front-facing string, up to 114 characters.

Echo uniqid (); You can see that uniqid is always a constantly changing hexadecimal number with a length of 13.
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 multiplied by 1048576 (2 of the Power of 20) and finally converted to hexadecimal.
Know Uniqid and time after the relationship uniqid can have a broader use, such as a text forum can use uniqid as the file name of the post.
In the post index, it is convenient to find posts by time.
Reference: hi.baidu.com/...7.html

Learning PHP, ask under what circumstances use Uniqid ()?

846189340160532f6c51bf26ab596f55

It's going to be used when detecting safety.

?>

You can use it when you're scrambling.

http://www.bkjia.com/PHPjc/904920.html www.bkjia.com true http://www.bkjia.com/PHPjc/904920.html techarticle php uniqid () function usage, phpuniqid function Usage This example describes the use of the Uniqid () function in PHP. Share to everyone for your reference. The specific method is analyzed as follows: Uniqid () function base ...

  • 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.