$token = md5(rand().time());
Has a good chance of never repeating.
- Time() does repeat within one second.
- Time() repeats for an hour once a year if it is on daylight savings.
- But rand() does not repeat for 2^30 steps.
- MD5 does not increase the randomness, and may even decrease it.
mt_rand()
is very good at "randomness", but that means that it can and will repeat -- at "random" times. Do not trust it for not repeating.
See also microtime(true)
; it is precise to the microsecond. But it still can lead to dups, especially if two different clients are using the same formula.
Simply use UUID functions. They have a lot of research and thought put into them. You are unnecessarily re-inventing the wheel. See this for why UUIDs mess with performance in a database and what to do about it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…