简体   繁体   中英

Generate a random unique ID using PHP

I have a table that is looks like this. This is some of the general information when a user registers on my site.

+------------+------------+
|   user_id  | username   |
|   312543   |   Bobby    |
|   543765   |  Victoria  |
+------------+------------+

I am just wondering, how would you generate a random unique number for user_id ? Let's say a number between 1 and 100 that is not yet in the database. I want to accomplish this using PHP, not SQL.

Improving upon Niet the Dark Absol's answer: You can use uniqid(rand()) . uniqid() will generate a number based on your server's internal clock and rand() will prefix it with a random number. So even if two users register in the same tiniest fraction, the rand() prefix will assign them a different prefix with over 99.99999% probability.

You could use PHP's uniqid function. It's not random (it's based on the server's clock) but unless you have two people register in the same tiniest fraction of a second then it will be guaranteed to be unique and it's non-sequential.

First of all, I agree with @Shankar Damodaran, it is a lot better to use AUTO_INCREMENT rather than random IDs because if you start using random ones, then you might have to check first whether that random ID already exists and then choose another random one and check again till you finally get a unique one.

And if you really want it to be random, you can use the PHP's rand function: http://www.php.net/manual/en/function.rand.php

function isUnique($var)
{
//we check here if var is unique, return TRUE on unique FALSE on non unique.
}

    //this is my function for generating random strings(can be used for numbers)
function _generateRandomString() {
    $chars = array_merge(range('a', 'z'), range(0, 9));
    shuffle($chars);
    return implode(array_slice($chars, 0,25));
}

  //actual code: 
$string= _generateRandomString();
while(!isUnique($string)) //if string is unique, while loop stops.
 {
 $string= _generateRandomString();
 }

PHP Doc :

Caution This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.

Warning This function does not guarantee uniqueness of return value. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread. Use more_entropy to increase likelihood of uniqueness.

 uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE ]] ) :
 string

In other words , if your script is running with parallel process , you risk to have the same unique id. so i can told you that there is no way to have 100% unique id , but we can increase the probability like using other function like : rand , and the more_entropy (the second parameter of uniqid)

 $unique = uniqid (rand(), true);

Don't forget that we are only increasing the probability to have unique id .

May be 99.99% uniqueness.

With php u can make like this.

$pattern = "1234567890";
$ID = $pattern{rand(0,10)};
for($i = 1; $i < 7; $i++)
{
    $ID .= $pattern{rand(0,10)};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM