PHP和mySQL(i):如何生成一个随机用户id?

时间:2022-11-24 22:55:54

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

说如果我想给每个用户在我的网站注册一个惟一的id。在我看来,如果我想我将不得不这样做:创建一个随机数字id,检查该id数据库中已经存在,如果它确实存在,那么创建另一个随机数和发送另一个查询,是否存在,等等…

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

这种情况可能会持续很长时间。除了有一个递增的id之外,还有什么合适的方法吗?

6 个解决方案

#1


3  

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();

最好的方法是通过auto increment函数,如果你不想使用这样的函数,你可以使用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.

基本上,它会生成一个基于毫秒的唯一id,如果你在函数中放入一个独特的前缀,它会生成一个非常独特的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.

这样,如果id已经存在或不存在,您就不必在生成id之后进行检查。你可以确定它是唯一的。

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

要了解更多信息,请查看此url http://php.net/uniqid!

#2


1  

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.

基本上,它会生成很多随机数来创建一个用于uniqueid的标记,sha就是这样。可能是开销,但是您可以确保从来没有生成一个双uid。

F*.

费边。

#3


1  

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

您可以使用rand()函数。它会在2之间产生一个随机数。

rand(0000,9999)

It will generate a number between 0 and 9999.

它将生成0到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...

我就是这么做的……

#4


0  

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.

如果你有一串15个数字,你将会看到999万亿,我怀疑它是否会运行“年龄”,因为地球上有近70亿人。

#5


0  

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.

ID是否需要为数字?通过切换到字母字符,你会得到更多的熵。6位数字是1,000,000个可能性,6个字符的字母数字字符串是2,176,782,336种可能性。使它混合情况字母数字和跳跃到156.25亿。

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.

您必须创建许多具有这种类型的id的项目,然后才能获得超过3或4个字符。

#6


0  

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

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

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

#1


3  

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();

最好的方法是通过auto increment函数,如果你不想使用这样的函数,你可以使用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.

基本上,它会生成一个基于毫秒的唯一id,如果你在函数中放入一个独特的前缀,它会生成一个非常独特的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.

这样,如果id已经存在或不存在,您就不必在生成id之后进行检查。你可以确定它是唯一的。

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

要了解更多信息,请查看此url http://php.net/uniqid!

#2


1  

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.

基本上,它会生成很多随机数来创建一个用于uniqueid的标记,sha就是这样。可能是开销,但是您可以确保从来没有生成一个双uid。

F*.

费边。

#3


1  

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

您可以使用rand()函数。它会在2之间产生一个随机数。

rand(0000,9999)

It will generate a number between 0 and 9999.

它将生成0到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...

我就是这么做的……

#4


0  

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.

如果你有一串15个数字,你将会看到999万亿,我怀疑它是否会运行“年龄”,因为地球上有近70亿人。

#5


0  

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.

ID是否需要为数字?通过切换到字母字符,你会得到更多的熵。6位数字是1,000,000个可能性,6个字符的字母数字字符串是2,176,782,336种可能性。使它混合情况字母数字和跳跃到156.25亿。

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.

您必须创建许多具有这种类型的id的项目,然后才能获得超过3或4个字符。

#6


0  

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

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

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