Home  >  Article  >  Backend Development  >  PHP generate unique ID

PHP generate unique ID

angryTom
angryTomforward
2019-10-23 17:22:463259browse

Preface

The 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, and the best solution is to combine the md5 function to generate a unique ID.

Use function

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

Get a prefixed unique ID based on the number of microseconds in the current time. prefix Useful parameter. For example: if on multiple hosts a unique ID might be generated in the same microsecond. If prefix is ​​empty, the length of the returned string is 13. If moreentropy is TRUE, the length of the returned string is 23. moreentropy 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 to generate non-repeating unique identifiers

This method will generate a large amount of repeated data. Running the following PHP code will generate the unique identifier and the corresponding element value in the array index. 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 identifiers

The 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 non-duplicate unique identification method three

There is no duplication in the unique identification 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 four

Use the sessioncreateid() function to generate a unique identifier. After actual testing, it was found that even if sessioncreateid() is called cyclically 100 million times, the There has been no duplication. php sessioncreateid() is a new function in PHP 7.1, which is used to generate session id. It cannot be used in lower versions.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of PHP generate unique ID. 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