简体   繁体   中英

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?

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 !

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...

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.

如果你有一个15个数字的字符串,你正在寻找高达999万亿,我怀疑它将在“年龄”运行,因为这个星球上有将近70亿人。

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.

我知道这个答案已经晚了,但最简单的解决方案是生成随机数,并确保它是100%唯一的

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

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