Using the PHP uniqid function to generate a unique id_php instance

Source: Internet
Author: User
Tags chr function prototype hash md5 unique id uuid

Scenarios that generate unique IDs are very common, such as temporary cache file names, temporary variables, temporary security codes, and so on, and the uniqid () function generates a unique ID based on the current time in microseconds. Because the generation of unique IDs is associated with microsecond time, the uniqueness of IDs is very reliable.

Generated unique ID The string returned by default has 13 strings long, if you do not define a prefix for a unique ID, you can return up to 23 strings long, and if you combine the MD5 () function, the resulting unique ID will be more reliable, and the most significant advantage of this generated ID is that it can be sorted, In particular, some of the values that need to be stored in the database.

One, function prototype

String Uniqid ([string prefix [, BOOL more_entropy]])

Prefix and length that can define unique IDs

Second, version compatible

PHP 3, PHP 4, PHP 5

The basic usage and example of function

1, generate a unique ID

<?php 
Echo uniqid (); 

2, combining the MD5 () function to generate a unique ID

<?php 
Echo MD5 (UNIQID ()); 

Output: dfbc5c8c6438de075da28b3c8a413fd0

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

<?php 
Echo uniqid (); 
Echo uniqid (); 
Echo Uniqid (); 

Output:

4bfd0e375396b
4bfd0e3753981
4bfd0e3753983

The result of the build is that the unique ID is sortable.
Using the Uniqid () function to generate a unique ID can be used both to generate a temporary ID and to generate a permanent unique ID (the storage database).

Ps:php several solutions for generating unique IDs

The following small series for everyone to organize three kinds of solutions, the specific contents are as follows:

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.

The W3school reference manual says: "The IDs generated through this function are not optimal because of the system time." If you want to generate an absolutely unique ID, use the MD5 () function.

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

function uuid () {
  if (function_exists (' Com_create_guid ')) {return
    com_create_guid ();
  } else {
    Mt_sra nd (double) microtime () * 10000); Optional for PHP 4.2.0 and up. Sow the seeds casually, 4.2.0 do not need them later.
    $charid = Strtoupper (MD5 (Uniqid (rand (), True)),//////////////////////////////
    $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, a);
    . chr ()//"}" return
    $uuid;
  }
}

Com_create_guid () is a PHP-generated unique ID method, after php5 seems to have gone.

3, the official uniqid () the reference manual has the user to provide the method, the result is 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; }

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.