PHP generates a unique ID

Foreword

PHP uniqid () function can be used to generate a unique identifier is not repeated, the function based on the current time stamp microsecond. At high concurrent or long extremely short interval (e.g., cyclic code), the large number of duplicate data occurs. Even with the second parameter will be repeated, the best solution is a combination md5 function to generate a unique ID.

Use function

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

Obtaining a prefix, based on the current time of several microseconds unique ID. prefix useful parameters. For example: if the unique ID may be generated on the same microseconds multiple hosts. prefix is ​​empty, the length of the string 13 is returned. moreentropy is TRUE, then the length of the string 23 is returned. moreentropy If set to TRUE, uniqid () will add additional fan at the end of the returned string (using the combined linear congruential generator). It makes a unique ID more unique.

PHP uniqid () method to generate a unique identification will not be repeated

This method will produce large amounts of repeated data, run the following PHP array index code is a unique identifier generated by the element values ​​corresponding to the unique identification number of repetitions.

<?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 () method to generate a unique identification will not be repeated two

This method generates a unique identification significantly reduced amount of repetition.

<?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){ $duplicates[$k]=$v; } } echo '<pre>'; print_r($duplicates); echo '</pre>'; ?>

PHP uniqid () method to generate a unique identification will not be repeated three

Repeat this method does not uniquely identify generated.

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

PHP uniqid () method to generate a unique identification will not be repeated four

Use sessioncreateid () function generates a unique identifier, after the actual test found that, even if the cycle call sessioncreateid () one hundred million times, there have been no repeat. php sessioncreateid () function is new php 7.1, used to generate the session id, lower version can not be used

Guess you like

Origin www.cnblogs.com/zmdComeOn/p/11701082.html