How can you generate a unique id in php?

PHP is one of the more popular programming languages in the world.

First released in 1995 as a way to build dynamic web applications, PHP is a dynamically typed scripting language with similarities to Perl and C. Although considered a general purpose programming language, PHP is primarily used for building websites.

How to Generate a UUID in PHP

PHP does not have built-in support for generating RFC 4122 compliant UUIDs. The function, uniqid(), is not a sufficient replacement for generating RFC 4122 compliant UUIDs as it may not always generate a unique value (especially if your computer is fast) and it can only generate a maximum of 23 characters.

However, below you'll find two, viable 3rd-party solutions for generating RFC 4122 compliant UUIDs in PHP.

1. Roll-Your-Own UUID Generation Function in PHP

This small helper function generates RFC 4122 compliant Version 4 UUIDs.

function guidv4($data = null) {

// Generate 16 bytes (128 bits) of random data or use the data passed into the function.

$data = $data ?? random_bytes(16);

assert(strlen($data) == 16);

// Set version to 0100

$data[6] = chr(ord($data[6]) & 0x0f | 0x40);

// Set bits 6-7 to 10

$data[8] = chr(ord($data[8]) & 0x3f | 0x80);

// Output the 36 character UUID.

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));

}

Notes

  • This function requires PHP 7 or later because of the use of the function random_bytes on line #3.
  • If you're using PHP 5 or earlier and have the openssl extension installed, you could use openssl_random_pseudo_bytes(16), instead of random_bytes(16) on line #3.
  • Credits go to this answer on Stackoverflow for this solution.
  • An example usage of this function would look like this:

    $myuuid = guidv4();

    echo $myuuid;

2. Use the ramsey/uuid PHP Library

If you're using Composer for managing dependencies in your PHP project, you can use the ramsey/uuid PHP library for generating UUIDs.

To get started with the library, you can install and add it to your project like this:

% composer require ramsey/uuid

Now you can use it in your PHP project. Here is a small example of generating a UUID with this library:

use Ramsey/Uuid/Uuid;

$myuuid = Uuid::uuid4();

printf("Your UUID is: %s", $myuuid->toString());


How can we improve this page? Let us know!



PHP Doc :

Caution This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.

Warning This function does not guarantee uniqueness of return value. 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. Use more_entropy to increase likelihood of uniqueness.

 uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE ]] ) :
 string

In other words , if your script is running with parallel process , you risk to have the same unique id. so i can told you that there is no way to have 100% unique id , but we can increase the probability like using other function like : rand , and the more_entropy (the second parameter of uniqid)

 $unique = uniqid (rand(), true);

Don't forget that we are only increasing the probability to have unique id .

May be 99.99% uniqueness.

What is unique ID in PHP?

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.

How do you generate unique identifiers?

The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.

Which of the following PHP functions can be used for generating unique ids?

Which of the following PHP functions can be used for generating unique ids? Explanation: The function uniqueid() is used to generate a unique ID based on the microtime (current time in microseconds).