How PHP generates a _php identifier technique

Source: Internet
Author: User
Tags md5 sha1 unique id uuid

This example describes how PHP generates a method that does not duplicate identifiers. Share to everyone for your reference. The implementation methods are as follows:

To generate a unique, duplicate identity we are mainly based on the current time and then converted to the MD5 value, so that almost guarantee the uniqueness of the label, following the collation of a number of PHP generated not duplicate identifier program code, interested friends can look at

PHP has a function that generates a unique ID: uniqid (), which is based on the current number of microseconds , and uses the following:

Copy Code code as follows:
Echo Uniqid (); 13-bit string
Echo uniqid ("Php_"); Of course, you can prefix it.
Echo uniqid ("Php_", TRUE); Generates a 23-bit string if the second argument more_entropy true

But the identity it generates may not be unique, so many people will:
Copy Code code as follows:
<?php
This is the first simple method, of course, using the SHA1 () function is also possible.
echo MD5 (UNIQID ());
The second is to use the time stamp method
echo MD5 (Time (). Mt_rand (1,1000000));
?>

Example:
Copy Code code as follows:
?
Generate a 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 code as follows:
<?php
$newhash = Create_unique ();
Echo $newhash;
?>

I've seen a lot of people using the MD5 () function, even if it doesn't exactly mean that purpose:
Copy Code code as follows:
Generate unique string
echo MD5 (Time (). Mt_rand (1,1000000));
There is actually a PHP function named Uniqid (), which is meant to, used for this.
Generate unique string
Echo Uniqid ();
/* Prints
4bd67c947233e
*/
Generate another unique string
Echo Uniqid ();
/* Prints
4bd67c9472340
*/

You may notice that although the string is unique, the first few characters are similar because the generated string is related to the server time.

But there is also a friendly aspect to it, because each newly generated ID is sorted alphabetically, so that the sorting becomes simple.

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

Copy Code code 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 PHP generates a globally unique identifier (GUID)

GUIDs are unique in space and time, ensuring that different numbers are produced at the same time and in different places.
No two computers in the world will generate duplicate GUID values.
When the GUID is required, it can be automatically generated by the algorithm, without the need of an authoritative authority to manage it.
The GUID is fixed in length and relatively short, and is ideal for sorting, labeling, and storage.

Copy Code code as follows:
<?php
PHP Build 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, 4). $hyphen
. substr ($charid, 4). $hyphen
. substr ($charid, 20,12);
return $uuid;
}
?>

I hope this article will help you with your PHP program design.

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.