0

我找不到实际函数的代码,因为我对它如何创建 id 感兴趣,例如它从哪里获取时间。

4

4 回答 4

3

如果您仍然对源代码感兴趣,它位于公共 git 存储库中: https ://github.com/php/php-src/blob/master/ext/standard/uniqid.c

gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
sec = (int) tv.tv_sec;
usec = (int) (tv.tv_usec % 0x100000);

/* The max value usec can have is 0xF423F, so we use only five hex
* digits for usecs.
*/
if (more_entropy) {
    spprintf(&uniqid, 0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
} else {
    spprintf(&uniqid, 0, "%s%08x%05x", prefix, sec, usec);
}

RETURN_STRING(uniqid, 0);
于 2013-01-12T00:44:23.333 回答
2

哪个功能?我不相信unique_id()PHP 中有函数。的代码uniqid()ext/standard/uniqid.c中。

你可以通过缩小存储库在 Github 上搜索这种东西。例如,搜索repo:php/php-src uniqid将在 php repo 中找到对 uniqid 的引用。

在这个早先问题的答案中有一个这样的例子,以及其他 Github 搜索语法。

于 2013-01-12T00:43:58.300 回答
1

您可以在 https://github.com/php/php-src/blob/master/ext/standard/uniqid.c中找到 uniqid() 的源代码

于 2013-01-12T00:40:17.200 回答
0

这可能对手册有所帮助:http: //php.net/manual/en/function.uniqid.php

它解释了唯一性是从以微秒为单位的时间获取的。

于 2013-01-12T00:31:56.173 回答