Home > php教程 > PHP开发 > body text

Generate unique ID using PHP uniqid function

高洛峰
Release: 2016-12-28 15:32:22
Original
1960 people have browsed it

The application scenarios for generating unique IDs are very common, such as temporary cache file names, temporary variables, temporary security codes, etc. The uniqid() function generates a unique ID based on the current time in microseconds. Since generating a unique ID is tied to microsecond time, the uniqueness of the ID is very reliable.

The generated unique ID returns a string that is 13 strings long by default. If the prefix of the unique ID is not defined, it can return up to 23 strings long. If combined with the md5() function, The reliability of the generated unique ID will be higher. The biggest advantage of this generated ID is that it can be sorted, especially for some values ​​that need to be stored in the database.

1. Function prototype

string uniqid ( [string prefix [, bool more_entropy]] )
Copy after login

can define the prefix and length of the unique ID

2. Version compatible

PHP 3, PHP 4, PHP 5

3, basic function usage and examples

1, generate a unique ID

<?php
echo uniqid();
?>
Copy after login

2, combined with md5 () function generates a unique ID

<?php
echo md5(uniqid());
?>
Copy after login

Output: dfbc5c8c6438de075da28b3c8a413fd0

3, generate multiple unique IDs, because it is measured in microseconds

<?php
echo uniqid();
echo uniqid();
echo uniqid();
?>
Copy after login

Output:

4bfd0e375396b
4bfd0e3753981
4bfd0e3753983

Judging from the generated results , sortable among unique IDs.
Using the uniqid() function to generate a unique ID can be used to generate both temporary IDs and permanent unique IDs (storage database).

ps: Several solutions for php to generate unique ids

The following editor has compiled three solutions for you, the specific contents are as follows:

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

This method has a certain probability of duplication

2. PHP built-in function uniqid()

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

There is a sentence in the w3school reference manual: "Because it is based on system time, the ID generated by this function is not optimal. If you need to generate an absolute For a unique ID, use the md5() function".

The following method returns similar results: 5DDB650F-4389-F4A9-A100-501EF1348872

function uuid() {
  if (function_exists ( &#39;com_create_guid&#39; )) {
    return com_create_guid ();
  } else {
    mt_srand ( ( double ) microtime () * 10000 ); //optional for php 4.2.0 and up.随便数播种,4.2.0以后不需要了。
    $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); //根据当前时间(微秒计)生成唯一id.
    $hyphen = chr ( 45 ); // "-"
    $uuid = &#39;&#39; . //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;
  }
}
Copy after login

com_create_guid() is the only generated method that comes with PHP The id method seems to be gone after php5.

3. The official uniqid() reference manual has methods provided by users, and the results are similar: {E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}

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

For more articles related to using PHP uniqid function to generate unique ID, please pay attention to PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template