为什么 PHP 的 uniqid 函数只返回 13 位数字而不是 14 位?

发布于 2024-08-25 12:06:53 字数 733 浏览 7 评论 0原文

uniqid() 函数返回一个 13 位长的十六进制数。根据 php.net 站点中的规范,该函数使用 microtime 生成唯一的价值。

但是 microtime 返回字符串格式的数字,如下所示:

"0.70352700 12689396875"

基本上是微秒和自 1970 年以来经过的秒数。 这是一个 9+11 位十进制数。

将 20 十进制数转换为十六进制将得到16 位十六进制数,而不是 13 位数字。

我还想去掉“0”。部分似乎永远不会改变,而微秒部分的最后两位数字似乎始终保持“00”。这样做十进制数将只有 9+11-3 位长,但仍然将 17 位十进制数转换为十六进制时会得到 14 位十六进制数而不是 13。

我不感兴趣以其他方式获取唯一 ID 或更长/更短的唯一 ID!我只是问是否有人知道为什么 uniqid 仅返回 13 位数字。

这似乎毫无意义:如果 uniqid 返回比 microtime 少一位数字,则意味着 microtime 给出的结果更独特uniqid 返回的值的强>。

uniqid() function returns a 13 digits long hexadecimal number. According to the spec in php.net site, the function uses microtime to generate the unique value.

But microtime returns numbers in string format as the following one:

"0.70352700 12689396875"

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

Converting a 20 decimal number into hex would result in a 16 digits hexadecimal NOT a 13 digits one.

I also thought to take out the "0." part that seem to never change, and the last two digits of the microsec part that seem to remain always "00". Doing this the decimal number would be only 9+11-3 digits long, but still a decimal number of 17 digits when converted into hex would result in 14 digits hexadecimal number NOT 13.

I'M NOT INTERESTED IN GETTING A UNIQUE ID IN ANOTHER WAY OR A LONGER/SHORTER UNIQUE ID! I'M ONLY ASKING IF SOMEONE KNOWS WHY DOES uniqid RETURNS ONLY 13 DIGITS.

It seems nosense: if uniqid returns one digit less than microtime, it means that microtime gives out results that are more unique of the ones returned by uniqid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩世 2024-09-01 12:06:53

http://www.php.net/manual/en/ 上找到了这个function.uniqid.php#95001

对我来说很有意义。如果需要解释请评论

郑重声明,底层
uniqid() 的函数似乎是
大致如下:

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

换句话说,前 8 个十六进制字符 =
Unixtime,最后 5 个十六进制字符 =
微秒。这就是为什么它有
微秒精度。还有,它
提供了一种方法
对时间进行逆向工程
uniqid 已生成:

日期(“r”,hexdec(substr(uniqid(),0,8)));

越往下走就越明显
字符串,数字变成“more
随着时间的推移,“独一无二”,但例外
数字 9,其中数字流行度
是 0..3>4>5..f,因为
10^6 和 16^5 之间的差异(这
对于其余的大概是正确的
数字也一样,但少得多
值得注意)。

Found this on http://www.php.net/manual/en/function.uniqid.php#95001

Makes sense to me. Comment if you need an explanation

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

旧城烟雨 2024-09-01 12:06:53

我认为 uniqid 生成的数字是基于 当前时间(以微秒为单位)——但不是那个时间:计算可能比您想象的要困难一些。

不过,如果您需要超过 13 位数字,您可以将 true 作为第二个参数传递给 uniqid,以获得更多熵 - 它会给您一个字符串23 个字符长。

For instance, with this portion of code :

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

我刚刚得到:

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

I think the number generated by uniqid is based on the current time in microseconds -- but it is not that time : the calculation is probably a bit harder that you think.

Still, if you need more than 13 digits, you can pass true as a second parameter to uniqid, to have more entropy -- and it'll give you a string that's 23 characters longs.

For instance, with this portion of code :

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

I just got :

string '4ba284384e4ca' (length=13)
string '4ba284384e4df9.73439132' (length=23)
独享拥抱 2024-09-01 12:06:53

对于 32 个字符的唯一 ID,请尝试以下操作:

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

要缩短它,只需使用 substr():

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

For a 32 character unique ID try this:

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

To shorten it just use substr():

$unique = substr(uniqid(rand(), true), 16, 16); // 16 characters long
不气馁 2024-09-01 12:06:53

根据 PHP 手册,默认值为 13,当 $more_entropy 设置为 true 时,它​​将是 23,但您最终总是会得到 Unix 时间戳。

我经常使用 $uid = md5( uniqid() );

更新:
现在,这将为您提供 32 个角色,一生中发生碰撞的可能性最小。

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

Per the PHP manual the Default is 13 with the $more_entropy set to true it will be 23, but you will always end up with a Unix Timestamp.

I often use $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)); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文