Several solutions for generating a unique id in php _ PHP Tutorial

Source: Internet
Author: User
Php provides several solutions to generate a unique id. I checked it online and found many methods: 1. md5 (time (). mt_rand (); this method has a certain probability that there will be duplicates. 2. the php built-in function uniqid () has been checked online based on microseconds. There are many methods.

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.

The code is as follows:


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 it 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}

The code is as follows:


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 ('ripmd128 ', $ 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;
}

Md5 (time (). mt_rand (); this method has a certain probability of repetition. 2. the php built-in function uniqid () is based on microseconds...

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.