PHP generation of non-repeated identifiers

Source: Internet
Author: User
This article describes how to generate non-repeated identifiers in PHP, which involves md5 conversion of time functions, uniqid () functions, and GUID applications, for more information, see

This article describes how to generate non-repeated identifiers in PHP, which involves md5 conversion of time functions, uniqid () functions, and GUID applications, for more information, see

This example describes how PHP generates non-repeated identifiers. Share it with you for your reference. The specific implementation method is as follows:

To generate unique and non-repeated identifiers, we mainly convert them to the md5 value based on the current time. This ensures the uniqueness of tags, the following describes some program code for generating non-repeated identifiers in PHP. If you are interested, let's take a look.

PHP comes with a function to generate a unique id: uniqid (), which is based on the number of microseconds of the current time. Its usage is as follows:

The Code is as follows:

Echo uniqid (); // a 13-Bit String
Echo uniqid ("php _"); // Of course you can add a prefix
Echo uniqid ("php _", TRUE); // if the second parameter more_entropy is true, a 23-Bit String is generated.


However, the IDS it generates may not be unique, so many people will:

The Code is as follows:

<? Php
// This is the first simple method. Of course, you can use the sha1 () function.
Echo md5 (uniqid ());
// Method 2, using the timestamp
Echo md5 (time (). mt_rand (1,000000 ));
?>


Example:

The Code is as follows:

<?
// Generate a unique identifier
// Sha1 () function, "Security 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;
}
?>


Example:

The Code is as follows:

<? Php
$ Newhash = create_unique ();
Echo $ newhash;
?>


I have seen many people use the md5 () function, even though it does not completely mean this purpose:

The Code is as follows:

// Generate unique string
Echo md5 (time (). mt_rand (1,000000 ));
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
*/


Although the string is unique, the first few characters are similar, because the generated string is related to the server time.

But in fact, there is also a friendly aspect, because each newly generated ID will be sorted in alphabetical order, so the sorting becomes very simple.

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

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_4bd67da% B %.4%4647
*/


This function will generate strings shorter than md5 (), saving some space.

Php generates a globally unique identifier (GUID)

GUID is unique in space and time to ensure that 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 automatically generated by an algorithm and not managed by an authority.
The GUID has a fixed length and is relatively short and small. It is suitable for sorting, identification, and storage.

The Code is as follows:

<? Php
// 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 help you with php programming.

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.