PHP code for generating non-repeated identifiers

Source: Internet
Author: User
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, I have sorted out some program code for generating non-repeated identifiers in PHP. I hope you can help me. PHP comes... 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, I have sorted out some program code for generating non-repeated identifiers in PHP. I hope you can help me.

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:

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, 23 digits are generated.

String

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

 

Example.

 

Example:

 

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

// 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 a second parameter to increase:


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

 

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.