生成电子邮件确认的确认码
使用 PHP,有哪些方法可以生成可以存储在数据库中并用于电子邮件确认的随机确认码?我一辈子都想不出一种方法来生成可以从用户的个人资料中生成的唯一号码。这样我就可以使用一个函数使数字足够小以包含在 URL 中(参见此链接)。请记住,用户必须单击链接来“确认/激活”他/她的帐户。如果我不能使用数字,那么使用字母和数字也没有问题。
话虽如此,我尝试将用户名与“盐”一起进行哈希处理以生成随机代码。我知道一定有更好的方法,所以让我们听听。
Using PHP, what are some ways to generate a random confirmation code that can be stored in a DB and be used for email confirmation? I can't for the life of me think of a way to generate a unique number that can be generated from a user's profile. That way I can use a function to make the number small enough to be included in the URL (see this link). Remember, the user has to click on the link to "confirm/activate" his/her account. If I can't use numbers, I have no problems using both letters and numbers.
With that said, I've tried hashing the username along with a "salt" to generate the random code. I know there has to be a better way, so let's hear it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将是 32 个字母数字字符长并且是唯一的。如果您希望它更短,只需使用 substr():
生成随机数据的替代方法包括:
That will be 32 alphanumeric characters long and unique. If you want it to be shorter just use substr():
Alternative methods to generate random data include:
1) 在数据库中创建激活字段
2) 注册后发送电子邮件
3) 创建要包含在电子邮件中的链接,使用唯一标识符
它看起来像这样
欢迎用户名感谢您注册。
请点击下面的链接激活您的帐户
4) 将激活字段更新为 true
(来源:jackborn.com)
1) Create an Activated Field in Database
2) After registration the Email is sent
3) Create a Link to include in Email,Use a Unique identifier
It would look something like this
Welcome Username Thanks for registering.
Please Click on the Link below to activate your account
4) Update the Activated Field to true
(source: jackborn.com)
接受的答案建议使用 PHP 的
uniqid()
的哈希值。 uniqid 文档明确警告它不会创建“随机或不可预测的字符串” ”,并强调“此函数不得用于安全目的。”如果担心确认码被猜测的可能性(这就是发出代码的全部意义) )您可能希望使用更随机的生成器,例如
openssl_random_pseudo_bytes()
。然后,您可以使用 bin2hex() 将其转换为漂亮的字母数字。以下内容看起来就像约翰·康德(John Conde)答案的输出,但(据说)更加随机且不易猜测:最新附录:正如奥列格·阿布拉扎耶夫(Oleg Abrazzaev)指出的那样,如果您想确保您的系统实际上有能力为了在运行时生成加密的强随机值,openssl_random_pseudo_bytes 接受对 bool 的引用来报告这一点。来自 phpinspectionsea 文档 的代码
:像以前一样使用生成的随机值:
The accepted answer suggests using a hash of PHP's
uniqid()
. The documentation for uniqid explicitly warns that it does not create "random nor unpredictable strings", and states emphatically that "This function must not be used for security purposes."If there is any concern over the possibility of a confirmation code being guessed (and that is the whole point of issuing a code) you may wish to use a more random generator such as
openssl_random_pseudo_bytes()
. You can then usebin2hex()
to turn it into a nice alphanumeric. The following looks just like the output of John Conde's answer, but is (supposedly) more random and less guessable:Late addendum: As Oleg Abrazhaev points out, if you want to make sure your system is actually capable of generating cryptographically strong random values at runtime,
openssl_random_pseudo_bytes
accepts a reference to a bool to report this. Code from phpinspectionsea docs:Then use the generated random value as before:
决定我需要一些更强大和附加功能的东西。这就是我想出的办法。
Decided I need something a little more robust and added functionality. So this is what I came up with.
使用这段小代码,您可以生成一个随机数,范围为 7 到 11 个数字。
使用php函数:
在这个函数中传递参数:
rand();
并返回;
示例:
我认为很难重复这个数字,因为它永远不会是同一天、同一小时或同一毫秒时间。
With this little code, you can generate a random number, with a range of 7 to 11 numbers.
Using php functions:
Passing parameters in this function:
rand ();
and return;
example:
I think it is difficult to repeat this number, for the reason it will never be the same day, nor the same hour, nor the same milliseconds of time.