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

Source: Internet
Author: User
PHP uniqid () function usage, phpuniqid function usage. PHP uniqid () function usage, phpuniqid function usage this article describes the usage of uniqid () function in PHP. Share it with you for your reference. The specific method analysis is as follows: uniqid () function based on PHP uniqid () function usage, phpuniqid function usage

This example describes how to use the uniqid () function in PHP. Share it with you for your reference. The specific method is analyzed as follows:

The uniqid () function generates a unique ID based on the current time in microseconds.
Note: Because the system time is used, the IDS generated through this function are not optimal. To generate an absolutely unique ID, use the md5 () function (please refer to the string function reference ).

The code is as follows:

<? Php
Echo uniqid ();
?>


In this example, a 32-character unique string is generated.

The code is 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.
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.

Let's take a look at the following code:

The code is as follows:

<? 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.
After you know the relationship between uniqid and time, uniqid can be used more widely. for example, you can use uniqid as The Post file name in a forum.

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

Based on the upstream and downstream code, 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.

The code is as follows:

<? Php
Function getRand (){
Return uniqid (). rand (1, 100000 );
}
Echo getRand ();
Exit;
?>

I hope this article will help you with PHP programming.


In php, help me explain the uniqid () function. I found it in English in the php Manual.

Uniqid
Generate only one value.
Syntax: string uniqid (string prefix );
Return value: string
Function type: encoding
Description
This function generates a unique string based on the current millisecond and specified prefix string. The prefix parameter is a prefix string that can contain up to 114 characters.

Echo uniqid (); we can see that uniqid is always a hexadecimal number with a continuously changing length of 13.
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.
Reference: hi.baidu.com/...7.html

When learning PHP, how can I use uniqid ()?

846189340160532f6c51bf26ab596f55

Used for security testing

?>

It will also be used when there is a random number.

Usage of the evaluate () function, phpuniqid function usage this article describes the usage of the uniqid () function in PHP. Share it with you 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.