PHP generation unique ID several workarounds _php tutorial

Source: Internet
Author: User
There's a lot of ways to check it online.

1, MD5 (Time (). Mt_rand (1,1000000));

This method has a certain probability that there will be repetition

2. PHP built-in function uniqid ()

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

W3school Reference Manual There is a sentence: "Due to system time, the ID generated by this function is not optimal." To generate an absolutely unique ID, use the MD5 () function.

The following method returns the result similar to: 5ddb650f-4389-f4a9-a100-501ef1348872

copy code code as follows:
function uuid () {
if (function_exists (' Com_create_guid ')) {
return com_create_guid ();
} else {
Mt_srand (double) microti Me () * 10000); Optional for PHP 4.2.0 and up. The random number sowing, 4.2.0 not need after.
$charid = Strtoupper (MD5 (Uniqid (rand (), true)), and//generates a unique ID based on the current time (in microseconds).
$hyphen = CHR;//"-"
$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;//"}"
return $uuid;
} The
}

Com_create_guid () is a PHP-generated unique ID method that appears to be out of PHP5.
3, the official uniqid () reference manual has the user-provided method, the result is similar: {E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}

Copy the Code code 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 (' 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;
}

http://www.bkjia.com/PHPjc/326742.html www.bkjia.com true http://www.bkjia.com/PHPjc/326742.html techarticle Online Search, there are a lot of methods 1, MD5 (Time (). Mt_rand (1,1000000)); This method has a certain probability of repeating 2, PHP built-in Functions uniqid () uniqid () function 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.