John Smith
John Smith

Reputation: 275

PHP & mySQL(i): How to generate a random user id?

say if I wanted to give every user that registered on my site a unique id. It seems to me that if I wanted to do this I would have to: Create a random number for the id, check to see if that id already exists in the database, if it does exist then create another random number and send yet another query to see if that exists, and so on...

This could go on for ages. Apart from having an incrementing id, is there any decent way to do this?

Upvotes: 7

Views: 18745

Answers (6)

Bynd
Bynd

Reputation: 705

I know it's late for this answer but the easiest solution is to generate random number and sure it will be unique 100% is

$uid = uniqid().date("Ymdhhis");

Upvotes: -1

Abhi Beckert
Abhi Beckert

Reputation: 33369

Does the ID need to be numeric? By switching to alphabetic characters you will get a lot more entropy. A 6 digit number is 1,000,000 posibilities, a 6 character alphanumeric string is 2,176,782,336 possibilities. Make it mixed case alphanumeric and that jumps to 15,625,000,000.

Here's how I usually generate unique strings that are as short as possible:

$chars = 'abcdefghijklmnopqrstuvwrxyzABCDEFGHIJKLMNOPQRSTUVWRXYZ0123456789';
mt_srand((double)microtime()*1000000);

$id = '';
do {
  $id .= $chars[mt_rand(0, strlen($chars) - 1)];
} while (isIdTaken($id));

var_dump($id);

You have to create a lot of items with this style of id, before you'll get to more than 3 or 4 characters.

Upvotes: 0

Michael
Michael

Reputation: 1886

If you have a string of 15 numbers you are looking at up to 999 trillion, I doubt it will run for "ages" considering there's almost 7 billion people on the planet.

Upvotes: 0

Frederick Marcoux
Frederick Marcoux

Reputation: 2223

You can use the rand() function. It will generate a random number between two.

rand(0000,9999)

It will generate a number between 0 and 9999.

To check if it already exist:

$id = rand(0000,9999);

/* CREATE YOUR MYSQL CONNECTION */
$user_list = mysql_query("SELECT * FROM users");
while ($user = mysql_fetch_array($user_list))
{
    if ($id == $user['id'])
    {
        echo('Already exist.');
    }
    else
    {
        /* YOUR CODE */
    }
}

It's the way I did it...

Upvotes: 1

Fabdrol
Fabdrol

Reputation: 738

First of all, I agree with the comments. It's all overhead code, and if you're using it to make it look interesting you should really reconsider your priorities.

But, if you still need it; here's a little something:

function uid() {
    mt_srand((double)microtime()*1000000);
    $token = mt_rand(1, mt_getrandmax());

    $uid = uniqid(md5($token), true);
    if($uid != false && $uid != '' && $uid != NULL) {
        $out = sha1($uid);
        return $out;
    } else {
        return false;
    }
}

Basically, it does a lot of random number generating to create a token for uniqueid, and then is sha's that. Probably overhead, but you can be sure that you never generate a double uid.

Fabian.

Upvotes: 1

Wesley
Wesley

Reputation: 2200

The best way to do this is via the auto increment function, if you really don't want to use a function like so you could use uniqid();

Basically you it generates an unique id based on milliseconds, if you put in a kinda unique prefix in the function it will generate a very unique id.

echo uniqid('prefix');

This way you won't have to check after generating an id, if it already exists or not. You can be sure it is unique.

For more information check this url http://php.net/uniqid!

Upvotes: 5

Related Questions