1. Home
  2. Computing & Technology
  3. PHP / MySQL

How To Generate a Unique ID

By , About.com Guide

Generating a unique ID for members of your site: Uniqid (prefix, more_entropy)

A unique site ID can be created in PHP using the uniqid () function. This function has two parameters we can set. The first is the prefix. This 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 is true then 23 characters will be returned.

Here are some examples:

<?php

//creates a unique id with the 'about' prefix
$a = uniqid(about);
echo $a;
echo "<br>";

//creates a longer unique id with the 'about' prefix
$b = uniqid (about, true);
Echo $b;
echo "<br>";

//creates a unique ID with a random number as a prefix - more secure than a static prefix
$c = uniqid (rand (),true);
echo $c;
echo "<br>";

//this md5 encrypts the username from above, so its ready to be stored in your database
$md5c = md5($c);
echo $md5c;
?>

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. Unique ID PHP - Generate Unique ID PHP - Unique username PHP

©2009 About.com, a part of The New York Times Company.

All rights reserved.