PHP: neden uniqid fonksiyonu sadece 13 basamak değil 14 verir?

4 Cevap php

uniqid() function bir 13 basamaklı uzun bir onaltılık sayı verir. Php.net sitesinde özelliklerine göre, fonksiyon benzersiz bir değer oluşturmak için microtime kullanır.

Ama microtime aşağıdaki biri olarak dize biçiminde döner numaraları:

"0.70352700 12689396875"

which are basically the microseconds and the seconds elapsed since 1970. This is a 9+11 digits decimal number.

Heks bir 20 ondalık sayı dönüştürme a 16 digits hexadecimal NOT a 13 digits one. ile sonuçlanacaktır

Ben de almaya düşündüm "0". asla değişmeyecek gibi görünüyor bir parçası ve her zaman "00" kalacak gibi görünüyor microsec kısmının son iki rakamı. Bu ondalık sayı yapmak sadece 9 11-3 basamak uzun, ama yine de a decimal number of 17 digits when converted into hex would result in 14 digits hexadecimal number NOT 13. olur

Ben başka bir yol ya A ARTIK / SHORTER EŞSİZ kimliği içine benzersiz bir kimlik ULAŞIM ilgilenmiyorum! BİRİSİ İade sadece 13 basamaklı uniqid ETMEZ NEDEN bilir ben sadece soruyorum.

Bu nosense görünüyor: uniqid daha tek haneli düşük olarak dönerse microtime, bu microtime daha benzersiz sonuçlar verir demektir of the ones returned by uniqid.

4 Cevap

http://www.php.net/manual/en/function.uniqid.php#95001 bu Bulundu

Bana mantıklı. Eğer bir açıklama gerekiyorsa Comment

For the record, the underlying function to uniqid() appears to be roughly as follows:

$m=microtime(true); sprintf("%8x%05x\n",floor($m),($m-floor($m))*1000000);

In other words, first 8 hex chars = Unixtime, last 5 hex chars = microseconds. This is why it has microsecond precision. Also, it provides a means by which to reverse-engineer the time when a uniqid was generated:

date ("r", hexdec (substr (uniqid (), 0,8)));

Increasingly as you go further down the string, the number becomes "more unique" over time, with the exception of digit 9, where numeral prevalence is 0..3>4>5..f, because of the difference between 10^6 and 16^5 (this is presumably true for the remaining digits as well but much less noticeable).

Ben mikrosaniye uniqid is based on şimdiki zaman tarafından oluşturulan sayı - düşünüyorum ama o zaman değil: hesaplama biraz daha sen düşünüyorum muhtemelen.

Birden fazla 13 basamak gerekir eğer Yine, size daha fazla entropi için uniqid için ikinci bir parametre olarak true geçirebilirsiniz - ve size 23 karakter olan bir dize vereceğim özlem.


For instance, with this portion of code :

var_dump(uniqid());
var_dump(uniqid('', true));

Ben sadece var:

string '4ba284384e4ca' (length=13)
string '4ba284384e4df9.73439132' (length=23)

32 karakter benzersiz bir kimlik için bu deneyin:

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

Sadece substr kullanmak kısaltmak için ():

$unique = substr(uniqid(rand(), true), 16, 16); // 16 characters long

PHP kılavuzun başına Varsayılan o 23 olacak ayarlamak true $ more_entropy 13 olduğunu, ancak her zaman bir Unix zaman damgası ile sona erecek.

Ben sık kullandığınız $uid = md5( uniqid() );

Update:
Now this will give you 32 characters with a minimal chance of collision is this lifetime.

md5(uniqid(mt_rand(), true));