High concurrent PHP uniqid non-duplicate unique identifier generation scheme

Source: Internet
Author: User
Tags unique id
The HP uniqid () function can be used to generate a unique identifier that is not duplicated, based on the current timestamp at the microsecond level. In the case of high concurrency or long intervals (such as looping code), there is a large amount of duplicate data. Even if the second parameter is used, it repeats, and the best scenario is to combine the MD5 function to generate a unique ID.
PHP uniqid () generates a method that does not duplicate unique identification

This method produces a large amount of duplicate data, running the following PHP code, the array index is the unique identifier produced, and the corresponding element value is the number of repetitions of that unique identity.[PHP]  View plain  copy <?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  ' <pre> ';            print_r ($duplicates);           echo  ' </pre> ';  ?>  
PHP uniqid () generate duplicate unique identification method two
This method generates a significant reduction in the number of unique identification duplicates.

[PHP]  View plain  copy <?php            $units  =  array ();           for ($i =0; $i <1000000; $i + +) {                     $units []  = uniqid (', true ');           }             $values   = array_count_values ($units);            $duplicates  = [];            foreach ($values  as  $k => $v) {                    if ($v >1) {                            &nBSP; $duplicates [$k]= $v;                    }           }            echo  ' <pre> ';            print_r ($duplicates);           echo  ' </ Pre> ';  ?>  
PHP uniqid () generate distinct unique identification method three
There are no duplicates 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 ' <pre> ';
Print_r ($duplicates);
Echo ' </pre> ';
?>

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.