Cara menggunakan php generate unique id

This tutorial explains the technique to generate unique identifiers that are extremely difficult to predict.

We'll be using the following PHP functions to generate the unique identifier:

  • rand
  • uniqid
  • md5

Use the code below to assign variable "id" the unique value. I'll discuss the code in more detail below.

$id = md5(uniqid(rand(), true));

After running the statement above PHP variable "id" will be assigned a unique 32 characters long value. Here is how it works:

The rand() function will generate a random number between 0 and getrandmax() (>= 32768). The uniqid function will take the random number as input and generate a unique identifier using the random value. After that we are taking the hash of the resultant unique identifier, which will give us a 32 characters (128 bits) long identifier.

A few sample runs gave me the following values:

da8b0432c9793b949d883a7ce4b5b038

c3ff02478937106b68733d9791586b54

116f84df8eb46a749060deee3daf53f9

Did this tutorial help a little? How about buy me a cup of coffee?

Cara menggunakan php generate unique id

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.

php Share on :

<?php
    //Using uniqid()
    $uid = uniqid();
    echo $uid;

    //Using random_bytes and bin2hex
    $ran_bytes = random_bytes(15);
    $uid = bin2hex($ran_bytes);
    echo $uid;

    //USING md5
    $str = "uname";
    $uid = md5($str);
?>

Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

To create a unique id using PHP, you can use inbuilt function uniqid() that generates the unique id based on current time in micro seconds. But it may not guarantee to be unique always. You can make it more unique using bin2hex() function or combination of that.

You can use bin2hex() method which can be useful to generate uniqe id. We are also using random_bytes(15) method to generate random bytes and passing it to bin2hex to generate a unique id.

In Php, we can create a unique id in PHP using uniqid() function. This is the best way to generate unique id in PHP, However we can say php generate unique id for mysql, php unique id generator anything.

Examples For Creating a Unique Id:-

1. //creates a unique id $id = uniqid(); 
   echo $a; echo "<br>";

2. //create a unique id with random $id = uniqid(rand(), true);
    echo $c;
3. //this md5 encrypts the username from above, so its ready to be stored in your database $md5c = md5($c); 
    echo $md5c;

How to upload excel and CSV file in CodeIgniter framework to MySQL

How can we get the IP address of the client in PHP?

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

    <?php

    $myuid = uniqid('gfg');

    echo $myuid;

    ?>

    Output:  

    gfg5b2b451823970

    Program 3: 

    php

    <?php

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

    echo $myuid;

    ?>

    Output:  

    gfg5b2b4555ab6bd7.27884925

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


    How can you generate a unique ID in 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.

    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.

    Is PHP timestamp unique?

    uniqid() in PHP generates a unique ID based on the current timestamp in microseconds.