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.
Parameters Used: The uiqid() function in PHP accepts two parameters.
- $prefix : It is an optional parameter which specifies a prefix to the unique id. It must be string.
- $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
- The uniqid() function tries to create unique identifier, but it does not guarantee 100% 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.
Below programs illustrate the uniqid() function:
Reference : http://php.net/manual/en/function.uniqid.php