Home  >  Article  >  Backend Development  >  PHP中的uniqid在高并发下的重复问题

PHP中的uniqid在高并发下的重复问题

WBOY
WBOYOriginal
2016-06-23 13:52:471152browse

最近项目中有用到生成token校验的问题。首先考虑用php中的uniqid()函数生成随机串,但是由于该函数好似基于微秒时间级别的。在高并发的情况下,就有可能会生成相同的值。

解决方案1:uniqid(rand(1,10000)), 该函数的第一个参数可用作生成数的前缀,如此,大大降低了生成数的重复率。但是重复的可能性还是存在的

解决方案2:md5(uniqid()),  使用md5()函数,可生成绝对唯一的值

Statement:
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