Home > Backend Development > PHP Tutorial > php automatically generates unique ids

php automatically generates unique ids

王林
Release: 2023-04-07 12:06:01
forward
3685 people have browsed it

php automatically generates unique ids

PHP uniqid() function can be used to generate a unique identifier that is not repeated, which is based on the current timestamp in microseconds. In cases of high concurrency or extremely short intervals (such as loop code), a large amount of duplicate data will appear. Even if the second parameter is used, it will be repeated. The best solution is to combine the md5 function to generate a unique ID.

Description

##string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )

Get a prefixed unique ID based on the current time microseconds.

Parameters

prefix

Useful parameters. For example: if on multiple hosts a unique ID may be generated in the same microsecond.

If prefix is ​​empty, the length of the returned string is 13. If more_entropy is TRUE, the length of the returned string is 23.

more_entropy

If set to TRUE, uniqid() will add extra entropy to the end of the returned string (using a combined linear congruential generator). Make the unique ID more unique.

PHP uniqid() Method 1 to generate non-duplicate unique identifiersThis method will generate a large amount of duplicate data. Running the following PHP code will result in the array index being the unique identifier generated, corresponding to The element value of is the number of times the unique identifier is repeated.

<?php        
$units = array();        
for($i=0;$i<1000000;$i++){                
$units[] = uniqid();        
}        
$values  = array_count_values($units);        
$duplicates = [];        
foreach($values as $k=>$v){                
if($v>1){                        
$duplicates[$k]=$v;                
}        
}        
echo &#39;<pre class="brush:php;toolbar:false">&#39;;        
print_r($duplicates);        
echo &#39;
'; ?>
Copy after login

PHP uniqid() Method 2 for generating non-duplicate unique identifiersThe amount of duplicate unique identifiers generated by this method is significantly reduced.

<?php        
$units = array();        
for($i=0;$i<1000000;$i++){                
$units[] = uniqid(&#39;&#39;,true);        
}        
$values  = array_count_values($units);       
$duplicates = [];        
foreach($values as $k=>$v){                
if($v>1){                        
$duplicates[$k]=$v;                
}        
}        
echo &#39;<pre class="brush:php;toolbar:false">&#39;;        
print_r($duplicates);        
echo &#39;
'; ?>
Copy after login

PHP uniqid() Generate unique identifier method threeThere is no duplication in the unique identifier generated by this method.

<?php        
$units = array();        
for($i=0;$i<1000000;$i++){                
$units[]=md5(uniqid(md5(microtime(true)),true));        
}        
$values  = array_count_values($units);        
$duplicates = [];        
foreach($values as $k=>$v){                
if($v>1){                        
$duplicates[$k]=$v;                
}        
}        
echo &#39;<pre class="brush:php;toolbar:false">&#39;;        
print_r($duplicates);        
echo &#39;
'; ?>
Copy after login

PHP uniqid() generates unique identifier method fourUse the session_create_id() function to generate a unique identifier. After actual testing, it was found that even if session_create_id() is called cyclically, 100 million times, there was no duplication.
php session_create_id() is a new function in PHP 7.1. It is used to generate session id. It cannot be used in lower versions.

The above content is for reference only!

Recommended video tutorial:

PHP video tutorial

The above is the detailed content of php automatically generates unique ids. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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