简体   繁体   English

使用PHP生成随机唯一ID

[英]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 ? 我只是想知道,你会如何为user_id生成一个随机的唯一编号? Let's say a number between 1 and 100 that is not yet in the database. 假设数据库中尚未包含1到100之间的数字。 I want to accomplish this using PHP, not SQL. 我想用PHP而不是SQL来实现这一点。

Improving upon Niet the Dark Absol's answer: You can use uniqid(rand()) . 改善Niet the Dark Absol的答案:你可以使用uniqid(rand()) uniqid() will generate a number based on your server's internal clock and rand() will prefix it with a random number. uniqid()将根据服务器的内部时钟生成一个数字, rand()将在其前面加上一个随机数。 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. 因此,即使两个用户在相同的最小部分中注册, rand()前缀也会为它们分配不同的前缀,概率超过99.99999%。

You could use PHP's uniqid function. 你可以使用PHP的uniqid函数。 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. 首先,我同意@Shankar Damodaran,使用AUTO_INCREMENT而不是随机ID要好得多,因为如果你开始使用随机ID,那么你可能必须首先检查该随机ID是否已经存在,然后选择另一个随机ID并再次检查,直到你最终得到一个独特的。

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 如果你真的希望它是随机的,你可以使用PHP的rand函数: 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 : PHP文档

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. 如果需要加密安全值,请考虑使用random_int(),random_bytes()或openssl_random_pseudo_bytes()。

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. 由于大多数系统通过NTP等调整系统时钟,因此系统时间会不断变化。 Therefore, it is possible that this function does not return unique ID for the process/thread. 因此,该函数可能不返回进程/线程的唯一ID。 Use more_entropy to increase likelihood of uniqueness. 使用more_entropy来增加唯一性的可能性。

 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. 换句话说,如果您的脚本使用并行进程运行,则您可能会拥有相同的唯一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) 所以我可以告诉你,没有办法拥有100%的唯一id,但我们可以像使用其他函数一样增加概率:rand和more_entropy (uniqid的第二个参数)

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

Don't forget that we are only increasing the probability to have unique id . 不要忘记我们只增加了拥有唯一身份证的可能性。

May be 99.99% uniqueness. 可能是99.99%的唯一性。

With php u can make like this. 用php你可以这样做。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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