Home  >  Article  >  Backend Development  >  php automatically generates unique ids

php automatically generates unique ids

王林
王林forward
2019-09-12 17:43:013576browse

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;
'; ?>

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;
'; ?>

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;
'; ?>

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:php array shuffled orderNext article:php array shuffled order