uniqid – PHP Function

Syntax :

uniqid (prefix, more_entropy) ;

Description :

The uniqid() function generates a unique ID based on the microtime (current time in microseconds). This is an inbuilt function of PHP.

ID created generated from the function is not unique because 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.

Note: The generated ID from this function is not optimal, because it is based on the system time. To generate an extremely difficult unique ID, use the md5() function.

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description

prefix, Optional, String , It specifies a prefix(user generated) to the unique ID and its useful when two scripts generate ids at exactly the same microsecond.
more_entropyOptional,  Boolean, Specifies more entropy at the end of the return value. This will make the result more unique. When set to TRUE then return string will be 23 characters. Default is FALSE and the return string will be 13 characters long.

[/table]


Output :

It returns the unique value as a string.


ChangeLog :

[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Version, Description
PHP 5.0 , The prefix parameter became optional.
PHP 4.3.1,The limit of 114 characters long for prefix was raised in this version.
[/table]


uniqid() – PHP Functions Example 1 :

<?php 
// This will generate unique id like this 5bbf0518c4013
echo uniqid();  
?>

Output of the program will be different each time when you run the code based on time of your system. Below will be the output when you run the function

5bbf0518c4013

uniqid() – PHP Functions Example 2 : Use of Prefix
<?php 
// This will generate unique id with prefix tutorialmines
$strExample = uniqid(tutorialmines); 
  
echo $strExample; 
?>

The following code in written in the file “test-frontf.txt” :

tutorialmines5bc03b4d4b651

uniqid() – PHP Functions Example 3 : Use of Prefix, entropy – TRUE
<?php 
// This will generate unique id with prefix tutorialmines
$strExample = uniqid(tutorialmines,TRUE); 
  
echo $strExample; 
?>

Output of the program will be different each time when you run the code based on time of your system. Below will be the output when you run the function

tutorialmines5bc03c54c2aab0.41881510

You may also like...