Logged in as: Guest | Login/Register
 

Generating Unique IDs in PHP

Tutorials > Web Development > PHP > Tips And Tricks > Generating Unique IDs in PHP

Discuss this tutorial here | Search www.geeksww.com for more tutorials here

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



 
Discuss this tutorial here | Search www.geeksww.com for more tutorials here
 

Similar Tutorials:

 

Support Geeks Worldwide:

Link to us:

You can support us by putting a link to our website on your blog or website (code is below).

<a href="http://www.geeksww.com/">
Geeks Worldwide - Tutorials about Software Installation,
Configuration, Administration, Monitoring, Tools, Tips &
Tricks
</a>

OR a simple one.

<a href="http://www.geeksww.com/">
Geeks Worldwide - IT related Tutorials
</a>

Feedbacks:

We appreciate feedbacks and suggestions about our tutorials and Geeks Worldwide from readers. Please contact us using the form here and let us know what you think about the tutorial and the website in general.

Bookmark Us:

We are working on new features for the website, please keep visiting or bookmark us using your favourite bookmarking service.

Subscribe to RSS:

You can subscribe to our RSS feed here.

 
Creative Commons License