Use the PHPuniqid function to generate a unique ID

Source: Internet
Author: User
The use cases for generating unique IDs are very common, such as temporary cache file names, temporary variables, and temporary security codes. the uniqid () function is based on the current time in microseconds, generate a unique ID. Because the generation of unique IDs is correlated with the microsecond time, the uniqueness of IDS is very reliable and the application scenarios for generating unique IDs are very common, such as temporary cache file names, temporary variables, and temporary security codes, the uniqid () function generates a unique ID based on the current time in microseconds. Because the unique ID generated is associated with the microsecond time, the uniqueness of the ID is very reliable.

By default, the generated unique ID returns a string of 13 characters. if the prefix of the unique ID is not defined, a maximum of 23 strings can be returned. if you combine the md5 () function, the generated unique ID has a higher reliability. this generated ID has the biggest advantage of sorting than the random ID, especially the values that need to be stored in the database.

I. function prototype

string uniqid ( [string prefix [, bool more_entropy]] )

You can define the prefix and length of a unique ID.

2. version compatibility

PHP 3, PHP 4, PHP 5

III. basic function usage and examples

1. generate a unique ID

<?php echo uniqid(); ?> 

2. use the md5 () function to generate a unique ID.

<?php echo md5(uniqid()); ?> 

Output: dfbc5c8c6438de075da28b3c8a413fd0

3. multiple unique IDs are generated, because they are measured in microseconds.

<?php echo uniqid(); echo uniqid(); echo uniqid(); ?> 

Output:

4bfd0e375108b
4bfd0e3753981
4bfd0e3753983

According to the generated results, unique IDs are sorted.
Using the uniqid () function to generate a unique ID can be used to generate both temporary IDs and permanent unique IDs (stored in the database ).

Ps: several solutions for php to generate a unique id

The following three solutions are provided:

1. md5 (time (). mt_rand (1,000000 ));

There is a probability that this method will be repeated.

2. php built-in function uniqid ()

The uniqid () function generates a unique ID based on the current time in microseconds.

W3school reference manual has one sentence: "Because the system time is based, the ID generated through this function is not the best. To generate an absolutely unique ID, use the md5 () function ".

The returned result of the following method is similar to: 5ddb417f-4389-f4a9-a100-501ef1348872.

Function uuid () {if (function_exists ('com _ create_guid ') {return com_create_guid () ;}else {mt_srand (double) microtime () * 10000 ); // optional for php 4.2.0 and up. no need to sow data as needed after 4.2.0. $ Charid = strtoupper (md5 (uniqid (rand (), true); // generate a unique id based on the current time (in microseconds. $ hyphen = chr (45); // "-" $ uuid = ''. // chr (123) // "{" substr ($ charid, 0, 8 ). $ hyphen. substr ($ charid, 8, 4 ). $ hyphen. substr ($ charid, 12, 4 ). $ hyphen. substr ($ charid, 16, 4 ). $ hyphen. substr ($ charid, 20, 12 );//. chr (125); // "}" return $ uuid ;}}

Com_create_guid () is the unique id method generated by php. it seems that php5 does not exist.

3. the official uniqid () reference manual has a user-provided method, the results are similar to: {E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}

public function create_guid($namespace = '') {    static $guid = '';  $uid = uniqid("", true);  $data = $namespace;  $data .= $_SERVER['REQUEST_TIME'];  $data .= $_SERVER['HTTP_USER_AGENT'];  $data .= $_SERVER['LOCAL_ADDR'];  $data .= $_SERVER['LOCAL_PORT'];  $data .= $_SERVER['REMOTE_ADDR'];  $data .= $_SERVER['REMOTE_PORT'];  $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));  $guid = '{' .       substr($hash, 0, 8) .      '-' .      substr($hash, 8, 4) .      '-' .      substr($hash, 12, 4) .      '-' .      substr($hash, 16, 4) .      '-' .      substr($hash, 20, 12) .      '}';  return $guid; }

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.