Home > Backend Development > PHP Tutorial > Use PHP uniqid function to generate unique ID, phpuniqid function id_PHP tutorial

Use PHP uniqid function to generate unique ID, phpuniqid function id_PHP tutorial

WBOY
Release: 2016-07-12 09:04:27
Original
991 people have browsed it

Use PHP uniqid function to generate unique ID, phpuniqid function id

The application scenarios for generating unique ID are very common, such as temporary cache file names, temporary variables, temporary security codes, etc., 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 generated unique ID will be more reliable. 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

You can define the prefix and length of the unique ID

Second, version compatible

PHP 3, PHP 4, PHP 5

3. Basic usage and examples of functions

1, generate a unique ID

<&#63;php 
echo uniqid(); 
&#63;> 
Copy after login

2, combined with the md5() function to generate a unique ID

<&#63;php 
echo md5(uniqid()); 
&#63;> 
Copy after login

Output: dfbc5c8c6438de075da28b3c8a413fd0

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

<&#63;php 
echo uniqid(); 
echo uniqid(); 
echo uniqid(); 
&#63;> 
Copy after login

Output:

4bfd0e375396b
4bfd0e3753981
4bfd0e3753983

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

ps: Several solutions to generate unique IDs in php

The editor below has compiled three solutions for you, the details are as follows:

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

This method has a certain probability of duplication

2. PHP built-in function uniqid()

The uniqid() 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 absolutely unique ID, please use the md5() function."

The following method returns similar results: 5DDB650F-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.随便数播种,4.2.0以后不需要了。
    $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); //根据当前时间(微秒计)生成唯一id.
    $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;
  }
}
Copy after login

com_create_guid() is PHP’s own method of generating a unique ID. It seems to be no longer available 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 = '') {  
  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;
 }
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1073132.htmlTechArticleUse the PHP uniqid function to generate a unique ID. The phpuniqid function id has very common application scenarios for generating unique IDs, such as temporary cache files. Name, temporary variable, temporary security code, etc., uniqid() function base...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template