How to use uniqid function in php to generate unique id

不言
Release: 2023-04-04 15:06:01
Original
4738 people have browsed it

uniqid in How to use uniqid function in php to generate unique id is a function that generates a unique value (ID) based on the current time. In the following article, we will introduce in detail the method of generating a unique id by the uniqid function in How to use uniqid function in php to generate unique id.

How to use uniqid function in php to generate unique id

Although uniqid in How to use uniqid function in php to generate unique id generates a unique value, it is based on the current time in microseconds on multiple servers at the same time, so the same value may be generated when running uniqid value.

This problem can be avoided by specifying a parameter using a prefix. It uses the rand function to assign a random value to the prefix.

Additionally, it can be used to upload images and files, such as generating filenames when saving uploaded files.

Note, uniqid is vulnerable to password system attacks, so do not use it as a password!

How to use uniqid

The first parameter is the prefix.

The second parameter specifies true/false whether to increase the number of characters to be generated. By default, the second parameter is false and the number of characters generated is 13 characters.

Let’s take a look at the specific writing method of uniqid

1. Confirm that it is different depending on the presence or absence of the first argument.

echo uniqid();          //  生成13个字符的字符串
echo uniqid("""");        // 指定参数为空,返回的字符串长度也为13
echo uniqid(""prefix_""); // 生成13个字符的字符串输出到“prefix_”
Copy after login

2. Set the second parameter

echo uniqid("""", false);          //生成13个字符的字符串
echo uniqid(""prefix_"", false);   // 在“prefix_”之后,输出由13个字符生成的字符串

echo uniqid("""", true);          // 生成23个字符的字符串
echo uniqid(""prefix_"", true);   // 在“prefix_”之后,输出由23个字符生成的字符串
Copy after login

3. Consider concurrent execution on multiple servers

echo uniqid(rand().'_');   // 在“rand()_”之后输出由13个字符生成的字符串
Copy after login

This article ends here. For more exciting content, you can pay attention to other column tutorials on the How to use uniqid function in php to generate unique id Chinese website! ! !

The above is the detailed content of How to use uniqid function in php to generate unique id. For more information, please follow other related articles on the PHP Chinese website!

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