Home  >  Article  >  Backend Development  >  How to generate unique identifiers in PHP, generate identifiers in php_PHP tutorial

How to generate unique identifiers in PHP, generate identifiers in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:36814browse

How PHP generates unique identifiers, PHP generates identifiers

The example in this article describes how PHP generates unique identifiers. Share it with everyone for your reference. The specific implementation method is as follows:

To generate unique and non-duplicate identifiers, we mainly convert it to the md5 value based on the current time. This can almost guarantee the uniqueness of the label. Here is a summary of some information about PHP generation of non-duplicate identifiers. Identifier program code, interested friends can take a look

PHP comes with a function that generates a unique id: uniqid(), which is based on the number of microseconds in the current time . The usage is as follows:

Copy code The code is as follows:
echo uniqid(); //13-digit string
echo uniqid("php_"); //Of course you can add the prefix
echo uniqid("php_", TRUE); //If the second parameter more_entropy is true, generate a 23-digit string

But the identifier it generates may not be unique, so many people will:
Copy code The code is as follows:
//This is the first simple method, of course you can also use the sha1() function.
echo md5(uniqid());
//Second method, using timestamp
echo md5(time() . mt_rand(1,1000000));
?>

Example:
Copy code The code is as follows:
//Generate unique identifier
//sha1() function, "Secure Hash Algorithm (SHA1)"
function create_unique() {
$data = $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']
.time() .rand();
Return sha1($data);
//return md5(time().$data);
//return $data;
}
?>

Examples are as follows:
Copy code The code is as follows:
$newhash = create_unique();
echo $newhash;
?>

I see a lot of people using the md5() function even though it's not exactly meant for this purpose:
Copy code The code is as follows:
// generate unique string
echo md5(time() . mt_rand(1,1000000));
There is actually a PHP function named uniqid() that is meant to be used for this.
// generate unique string
echo uniqid();
/* prints
4bd67c947233e
*/
// generate another unique string
echo uniqid();
/* prints
4bd67c9472340
*/

You may notice that although the strings are unique, the first few characters are similar. This is because the resulting strings are relative to the server time.

But there is actually a friendly side, since each newly generated ID will be arranged in alphabetical order, so sorting becomes simple.

To reduce the probability of duplication, you can pass a prefix, or a second parameter to increase:

Copy code The code is as follows:
// with prefix
echo uniqid('foo_');
/* prints
foo_4bd67d6cd8b8f
*/
// with more entropy
echo uniqid('',true);
/* prints
4bd67d6cd8b926.12135106
*/
// both
echo uniqid('bar_',true);
/* prints
bar_4bd67da367b650.43684647
*/

This function will produce a shorter string than md5(), saving some space.

How to generate a globally unique identifier (GUID) in php

GUID is unique in space and time, ensuring that different numbers generated in different places at the same time are different.
No two computers in the world will generate duplicate GUID values.
When a GUID is needed, it can be completely automatically generated by the algorithm and does not require an authoritative organization to manage it.
GUIDs have a fixed length and are relatively short, making them ideal for sorting, identification, and storage.

Copy code The code is as follows:
//php generates GUID
function getGuid() {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));

$hyphen = chr(45);// "-"
$uuid = substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12);
return $uuid;
}
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/915426.htmlTechArticleHow PHP generates unique identifiers, php generates identifiers This article describes how PHP generates unique identifiers method. Share it with everyone for your reference. The specific implementation method is as follows: Health...
Statement:
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