简体   繁体   中英

Unique ID with PHP

I write this simple line to get random & unique code each time (just 8 characters):


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

Output:

077331e5
5af425b1
0fc7dcf2
...

I ask if I'll never get a collision (duplicate). Or that can happen.

BS:
It's better to use 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 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.

Your "unique code" is a string of eight hexadecimal digits, and thus it has 4294967296 possible values. You are thus guanteed to get a duplicate of an earlier code by the 4294967297th time you run it.

PHP has a method to provide unique Ids called 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.

Short answer: it can happen. There's a discussion here about the collision space of MD5 that you might want to check out. Doing a substring of the MD5 will make the collision space much, much larger.

A better solution may be the answer proposed here , possibly checking it against other unique IDs that you've generated.

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

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