- Welcome to Geeksww.com
Generating Unique IDs in PHP
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:
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?
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.
tags cloud
popular searches
free download for mysql database server 5.1.5, bison, gearman, source code, php, install cairo, laptop, java, mysql, install mysql, linux, mysql initialization, mysql mysql, tools, ubuntu
Similar Tutorials:
- Simple Coding Style for PHP - Part 1/2
- How to check PHP version number?
- Seeing indexes in a MySQL table
- PHP Useful functions (Part 2) - The currentURL function
- How to delete rows in table when PK is referenced in another table
Tutorials in 'Web Development > PHP' (more):
- Generating prime nos in PHP using multiprocessing - Geaman with PHP client Part I
- PHP Useful functions (Part 2) - The currentURL function
- PHP Useful functions (Part 1) - The inRange function
- How to check PHP version number?
- Installing Symfony using PEAR
Comments (write a comment):
Great!! This is good example for generate unique ID in PHP. md5() function useful to me.This was so easy to follow and exactly what I was looking for.
Thanks. Posted by: Custom W on Aug 08, 2017
leave a comment