How to auto generate unique id in php?

❮ PHP Misc Reference


Definition and Usage

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

Note: The generated ID from this function does not guarantee uniqueness of the return value! To generate an extremely difficult to predict ID, use the md5() function.


Syntax

uniqid(prefix,more_entropy)

Parameter Values

ParameterDescription
prefix Optional. Specifies a prefix to the unique ID (useful if two scripts generate ids at exactly the same microsecond)
more_entropy Optional. Specifies more entropy at the end of the return value. This will make the result more unique. When set to TRUE, the return string will be 23 characters. Default is FALSE, and the return string will be 13 characters long

Technical Details

Return Value:Returns the unique identifier, as a string
PHP Version:4+
Changelog:The prefix parameter became optional in PHP 5.0.
The limit of 114 characters long for prefix was raised in PHP 4.3.1.

❮ PHP Misc Reference


How to Generate a Unique ID in PHP

Scott-Cartwright/Getty Images

Updated on October 02, 2018

A unique user ID can be created in PHP using the uniqid () function. This function has two parameters you can set.

The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy. If this is false or not specified, it will return 13 characters; if it's true, 23 characters will be returned.

Examples For Creating a Unique ID

Below are examples of creating a unique user ID, but each are a little different.

The first creates a normal unique ID while the second shows how to make a longer ID. The third example creates an ID with a random number as the prefix while the last line can be used to encrypt the username before storing it.

//creates a unique id with the 'about' prefix $a = uniqid(about); echo $a; echo "
";
//creates a longer unique id with the 'about' prefix $b = uniqid (about, true); Echo $b; echo "
";
//creates a unique ID with a random number as a prefix - more secure than a static prefix $c = uniqid (rand (),true); echo $c; echo "
";
//this md5 encrypts the username from above, so its ready to be stored in your database $md5c = md5($c); echo $md5c; ?>

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The uniqid() function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds (micro time). 
    The ID generated from the uniqid() function is not optimal since it is based on the system time and is not cryptographically secured. Thus it should not be for cryptographical purposes.
    The uniqid( ) function accepts prefix and more_entropy as parameters and returns timestamp based unique identifier as a string.
     

    Syntax:  

     uniqid($prefix, $more_entropy) 

    Parameters Used: The uiqid() function in PHP accepts two parameters. 

    1. $prefix : It is an optional parameter which specifies a prefix to the unique id. It must be string.
    2. $more_entropy : It is an optional parameter which specifies more entropy at the end of the return value which makes the id more unique.The default value is FALSE, which returns 13 characters long string whereas when it is set to TRUE, the return string is 23 characters long.

    Return Value: It returns timestamp based unique identifier as a string.
    Errors And Exceptions: 

    1. The uniqid() function tries to create unique identifier, but it does not guarantee 100% uniqueness of return value.
    2. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread.

    Below programs illustrate the uniqid() function:
    Program 1: 

    php

    Output:  

    3b2c662647f18

    Program 2: 

    php

    $myuid = uniqid('gfg');

    echo $myuid;

    ?>

    Output:  

    gfg5b2b451823970

    Program 3: 

    php

    $myuid = uniqid('gfg', true);

    echo $myuid;

    ?>

    Output:  

    gfg5b2b4555ab6bd7.27884925

    Reference : http://php.net/manual/en/function.uniqid.php


    How to generate unique user ID using PHP?

    A unique user ID can be created in PHP using the uniqid () function. This function has two parameters you can set. The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy.

    Which PHP function is used to generate unique ID?

    The uniqid() function generates a unique ID based on the microtime (the current time in microseconds). Note: The generated ID from this function does not guarantee uniqueness of the return value! To generate an extremely difficult to predict ID, use the md5() function.

    What is Uniqid PHP?

    The uniqid() function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds (micro time). The ID generated from the uniqid() function is not optimal since it is based on the system time and is not cryptographically secured.

    How does MySQL generate unique ID?

    This function in MySQL is used to return a Universal Unique Identifier (UUID) generated according to RFC 4122, “A Universally Unique Identifier (UUID) URN Namespace”. It is designed as a number that is universally unique. Two UUID values are expected to be distinct, even they are generated on two independent servers.