PHP generates a method of not repeating identifiers, PHP generates identifiers _php tutorial

Source: Internet
Author: User
Tags unique id

PHP generates a method that does not duplicate identifiers, and PHP generates identifiers


The examples in this article describe how PHP generates non-repeating identifiers. Share to everyone for your reference. The implementation method is as follows:

Generate unique non-duplicate identities we are mainly based on the current time and then converted in the MD5 value, so that almost can guarantee the uniqueness of the label, the following collation of some PHP generated non-repeating identifier program code, interested friends can see

PHP comes with a function that generates a unique ID: uniqid (), which is based on the number of microseconds in the current time, use the following:
Copy CodeThe code is as follows: Echo Uniqid (); A 13-bit string
Echo uniqid ("Php_"); Of course you can add a prefix.
Echo uniqid ("Php_", TRUE); Generates a 23-bit string if the second argument is more_entropy true
But the identity it generates may not be unique, so many people will:
Copy CodeThe code is as follows: <?php
This is the first simple method, and of course the SHA1 () function is also available.
echo MD5 (UNIQID ());
Second, the use of time-stamping methods
echo MD5 (Time (). Mt_rand (1,1000000));
?>
Example:
Copy CodeThe code is 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 CodeThe code is as follows: <?php
$newhash = Create_unique ();
Echo $newhash;
?>
I see a lot of people using the MD5 () function, even if it doesn't exactly mean this purpose:
Copy CodeThe code is as follows://Generate unique string
echo MD5 (Time (). Mt_rand (1,1000000));
There is actually a PHP function named Uniqid () which is meant to being 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 resulting string is related to the server time.

But in fact there is also a friendly aspect, since each newly generated ID is sorted alphabetically, so the ordering becomes simple.

To reduce the probability of repetition, you can pass a prefix, or a second parameter to add:
Copy the 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 different numbers are generated at different places at the same time.
No two computers in the world will generate duplicate GUID values.
When a GUID is required, it can be generated entirely automatically by the algorithm, without the need for an authoritative authority to manage it.
The length of the GUID is fixed and relatively short, and is ideal for sorting, identifying, and storing.
Copy the 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/915426.html www.bkjia.com true http://www.bkjia.com/PHPjc/915426.html techarticle PHP generates a method that does not duplicate identifiers, and PHP generates identifiers in this article, we describe how PHP generates non-repeating identifiers. Share to everyone for your reference. The concrete implementation method is as follows: Sheng ...

  • 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.