使用 PHP 生成随机唯一 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23032354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate a random unique ID using PHP
提问by Bono
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 uniquenumber 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.
我只是想知道,您将如何为 生成随机唯一编号user_id
?假设数据库中还没有一个介于 1 到 100 之间的数字。我想使用 PHP 而不是 SQL 来实现这一点。
回答by Crashh
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.
改进 Niet the Dark Absol 的回答:您可以使用uniqid(rand())
. uniqid()
将根据您服务器的内部时钟生成一个数字,并rand()
在其前面加上一个随机数。因此,即使两个用户以相同的最小分数注册,rand()
前缀也会以超过 99.99999% 的概率为他们分配不同的前缀。
回答by Niet the Dark Absol
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.
你可以使用 PHP 的uniqid
函数。它不是随机的(它基于服务器的时钟),但是除非您有两个人在同一秒的最短时间内注册,否则它将保证是唯一的并且是非顺序的。
回答by Always Learning Forever
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 是否已经存在,然后选择另一个随机 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
回答by Patrick
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();
}
回答by Mohammed Yassine CHABLI
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.
注意 此函数不会生成加密安全值,不应用于加密目的。如果您需要加密安全值,请考虑改用 random_int()、random_bytes() 或 openssl_random_pseudo_bytes()。
警告 此函数不保证返回值的唯一性。由于大多数系统通过 NTP 或类似方式调整系统时钟,因此系统时间不断变化。因此,此函数可能不返回进程/线程的唯一 ID。使用 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. 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)
换句话说,如果您的脚本与并行进程一起运行,您就有可能拥有相同的唯一 ID。所以我可以告诉你,没有办法拥有 100% 唯一的 id,但我们可以像使用其他函数一样增加概率:rand 和more_entropy(uniqid 的第二个参数)
$unique = uniqid (rand(), true);
Don't forget that we are only increasing the probability to have unique id .
不要忘记,我们只是在增加拥有唯一 id 的可能性。
May be 99.99% uniqueness.
可能是 99.99% 的唯一性。
回答by user2942910
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)};
}