php中利用uniqid() 函数生成唯一的id_PHP教程

WBOY
Release: 2016-07-13 16:53:55
Original
1095 people have browsed it

php中利用uniqid() 函数生成唯一的id

function createId($prefix = "")
{
    $str = md5(uniqid(mt_rand(), true));
    return $prefix . $str;
}

//
uniqid(prefix,more_entropy)
Copy after login

uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID。
 
prefix 可选。为 ID 规定前缀。如果两个脚本恰好在相同的微秒生成 ID,该参数很有用。
more_entropy 可选。规定位于返回值末尾的更多的熵。
 
如果 prefix 参数为空,则返回的字符串有 13 个字符串长。如果 more_entropy 参数设置为 true,则是 23 个字符串长。
如果 more_entropy 参数设置为 true,则在返回值的末尾添加额外的熵(使用组合线形同余数生成程序),这样可以结果的唯一性更好。
 
返回值
以字符串的形式返回唯一标识符。
 
提示和注释
注释:由于基于系统时间,通过该函数生成的 ID 不是最佳的。如需生成绝对唯一的 ID,请使用 md5() 函数(请在字符串函数参考中查找)
 
//
mt_rand() 使用 Mersenne Twister 算法返回随机整数。
mt_rand(min,max)
 
说明
如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 RAND_MAX 之间的伪随机数。例如想要 5 到 15(包括 5 和 15)之间的随机数,用 mt_rand(5, 15)。
很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。mt_rand() 函数是非正式用来替换它的。该函数用了 Mersenne Twister 中已知的特性作为随机数发生器,它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1053348.htmlTechArticlephp中利用uniqid() 函数生成唯一的id function createId($prefix = ){ $str = md5(uniqid(mt_rand(), true)); return $prefix . $str;}//uniqid(prefix,more_entropy) uniqid() 函数基...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!