如何生成随机的、唯一的字母数字字符串?

2022-01-30 00:00:00 string random php uniqueidentifier

如何使用数字和字母生成随机的唯一字符串以用于验证链接?就像您在网站上创建帐户时,它会向您发送一封带有链接的电子邮件,您必须单击该链接才能验证您的帐户

How would it be possible to generate a random, unique string using numbers and letters for use in a verify link? Like when you create an account on a website, and it sends you an email with a link, and you have to click that link in order to verify your account

如何使用 PHP 生成其中之一?

How can I generate one of those using PHP?

推荐答案

安全声明:此解决方案不应用于随机性质量可能影响应用程序安全性的情况.特别是 rand()uniqid() 不是加密安全的随机数发电机.请参阅 Scott 的回答 以获得安全的替代方案.

Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.

如果您不需要它随着时间的推移绝对独一无二:

If you do not need it to be absolutely unique over time:

md5(uniqid(rand(), true))

否则(假设您已经为您的用户确定了唯一的登录名):

Otherwise (given you have already determined a unique login for your user):

md5(uniqid($your_user_login, true))

相关文章