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.