简体   繁体   English

PHP的唯一ID

[英]Unique ID with PHP

I write this simple line to get random & unique code each time (just 8 characters): 我写这行简单代码来每次都获得随机且唯一的代码(仅8个字符):


echo substr(md5(uniqid(rand(), true)),0,8);

Output: 输出:

077331e5 077331e5
5af425b1 5af425b1
0fc7dcf2 0fc7dcf2
... ...

I ask if I'll never get a collision (duplicate). 我问我是否永远不会发生碰撞(重复)。 Or that can happen. 否则可能会发生。

BS: BS:
It's better to use time() ? 最好使用time()吗?


echo substr(md5(uniqid(time(), true)),0,8);

Hashes can have collisions. 哈希可能会发生冲突。 By taking a substring of the hash you are just upping the chance of that happening. 通过获取哈希的子字符串,您只是在增加发生这种情况的机会。

Regardless of what you feed into md5(), by doing the substring, you're eliminating a large part of md5's output and constricting the range of possible hashes. 不管您输入md5()的内容是什么,通过执行子字符串,您都将消除md5的大部分输出,并限制了可能的哈希范围。 md5 outputs a 128bit string, and you're limiting it to 32bits, So you've got from a 1 in 1x10^38 to 1 in 4 billion chance of a collision. md5输出一个128bit的字符串,而您将其限制为32bits,因此,发生碰撞的几率从1x10 ^ 38的1到40亿的1。

Your "unique code" is a string of eight hexadecimal digits, and thus it has 4294967296 possible values. 您的“唯一代码”是由八个十六进制数字组成的字符串,因此它具有4294967296个可能的值。 You are thus guanteed to get a duplicate of an earlier code by the 4294967297th time you run it. 因此,在第4294967297次运行该命令时,您会得到一个早期代码的副本。

PHP has a method to provide unique Ids called uniqid() PHP有一种提供唯一ID的方法,称为uniqid()

You stand a fair chance of your 8 char MD5 being unique but as with any random string the shorter you make the more likely you are to have a collision. 您很有可能会发现8位字符MD5是唯一的,但是与任何随机字符串一样,您越短越容易发生碰撞。

Short answer: it can happen. 简短的回答:可能会发生。 There's a discussion here about the collision space of MD5 that you might want to check out. 有一个讨论, 在这里了您可能要检查MD5碰撞的空间。 Doing a substring of the MD5 will make the collision space much, much larger. 对MD5进行子字符串处理将使碰撞空间大得多。

A better solution may be the answer proposed here , possibly checking it against other unique IDs that you've generated. 此处提出的答案可能是一个更好的解决方案,可能将其与您生成的其他唯一ID进行比较。

Your code returns part of a hash. 您的代码返回哈希的一部分。 Hashes are for hashing, thus you can not guarantee any pattern within the results (eg. uniqueness). 散列用于散列,因此您不能保证结果中的任何模式(例如,唯一性)。

Also, you are getting only part of a hash, and each letter from a hash is hexadecimal (from 0 to 9 or from a to b - 16 possibilities). 另外,您仅获得哈希的一部分,哈希中的每个字母均为十六进制(从09或从ab -16的可能性)。 It needs only a simple calculation: 它只需要一个简单的计算:

16 ^ 8 = 4 294 967 296

to find how many unique values can your code generate. 查找代码可以生成多少个唯一值。 This number ( 4 294 967 296 ) means, that if you use this function more thatn 4 294 967 296 times, the value generated with it surely will not be unique . 此数字( 4 294 967 296 )表示,如果您使用此功能的次数超过4 294 967 296次,则使用它生成的值肯定不会唯一 Of course it is certain, that in this case the number of iterations will not be unique after applying it to smaller number of values. 当然可以肯定的是,在这种情况下,将迭代次数应用于较小数量的值后,迭代次数将不是唯一的。

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

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