How to Generate a Unique ID in PHP

PHP Code on a screen shot with shallow depth of field

Scott-Cartwright/Getty Images

A unique user ID can be created in PHP using the uniqid () function. This function has two parameters you can set.

The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy. If this is false or not specified, it will return 13 characters; if it's true, 23 characters will be returned.

Examples For Creating a Unique ID

Below are examples of creating a unique user ID, but each are a little different.

The first creates a normal unique ID while the second shows how to make a longer ID. The third example creates an ID with a random number as the prefix while the last line can be used to encrypt the username before storing it.

//creates a unique id with the 'about' prefix $a = uniqid(about); echo $a; echo "
//creates a longer unique id with the 'about' prefix $b = uniqid (about, true); Echo $b; echo "
//creates a unique ID with a random number as a prefix - more secure than a static prefix $c = uniqid (rand (),true); echo $c; echo "
//this md5 encrypts the username from above, so its ready to be stored in your database $md5c = md5($c); e