Just to note this function is fairly slow, and can bring your script to a crawl if it is in a loop. Strangely if you run it as uniqid('', true) it runs much more quickly
(PHP 4, PHP 5, PHP 7, PHP 8)
uniqid — 生成基于时间的标识符
获取基于当前时间的标识符,精度为微秒,以指定 prefix
作为前缀,并可选择附加随机生成的值。
本函数并不会生成安全加密的值,并且不可用于加密或者要求返回值不可猜测的目的。
如果需要加密安全随机,则可以将 Random\Engine\Secure 引擎用于 Random\Randomizer。对于简单的用例,random_int() 和 random_bytes() 函数提供了操作系统的 CSPRNG 支持的方便且安全的 API。
该函数不保证返回值的唯一性,因为该值基于当前时间(以微秒为单位)或当前时间加上少量随机数据(如果
more_entropy
为 true
)。
返回字符串形式的,基于时间戳的标识符。
该函数不保证返回值的唯一性。
示例 #1 uniqid() 示例
<?php
/* 一个 uniqid,像:4b3403665fea6 */
printf("uniqid(): %s\r\n", uniqid());
/* 也可以为 uniqid 添加前缀,以下两种方式相同:
*
* $uniqid = $prefix . uniqid();
* $uniqid = uniqid($prefix);
*/
printf("uniqid('php_'): %s\r\n", uniqid('php_'));
/* 还可以启用 more_entropy 参数,在
* 某些系统上是必须的,比如 Cygwin。这使得 uniqid()
* 产生如下值:4b340550242239.64159797
*/
printf("uniqid('', true): %s\r\n", uniqid('', true));
?>
注意:
在 Cygwin 环境下,为了使此函数能够工作,
more_entropy
必须设置为true
。
Just to note this function is fairly slow, and can bring your script to a crawl if it is in a loop. Strangely if you run it as uniqid('', true) it runs much more quickly