PHP:如何生成一个随机的,唯一的字母数字字符串? [英] PHP: How to generate a random, unique, alphanumeric string?

查看:322
本文介绍了PHP:如何生成一个随机的,唯一的字母数字字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...yeah...one of those.

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

How can I generate one of those using PHP?

更新:刚刚记得有关 uniqid() 的内容.这是一个PHP函数,它根据当前时间(以微秒为单位)生成唯一的标识符.我想我会用.

Update: Just remembered about uniqid(). It's a PHP function that generates a unique identifier based on the current time in microseconds. I think I'll use that.

推荐答案

安全声明:在您的随机性会影响应用程序安全性的情况下,不应使用此解决方案.特别是, rand()uniqid()并不是加密安全的随机数生成器.请参阅斯科特的答案以获取安全的替代方法.

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))

这篇关于PHP:如何生成一个随机的,唯一的字母数字字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆