如何使用PHP生成加密安全的随机数?
问题描述:
可能重复:
在PHP中安全生成随机数字
以生成用作认证令牌的密码随机字符串,其将绑定到数据库中的会话数据。我们使用PHP,它似乎没有一个合适的随机数生成器内置。我们如何使用php生成一个长度为N的加密安全的随机字符串?
We need to generate a cryptographically random string to use as an authentication token, which will be tied to session data in the database. We are using PHP, which doesn't appear to have a suitable random number generator built-in. How can we generate a cryptographically secure random string of N length using php?
还要注意,由于我们的应用程序的本质,shell_exec不在表中。
Also note, due to the nature of our application, shell_exec is off the table.
答
uniqid
? docs有一个如何用于cookie /会话的示例。
what about uniqid
? docs have an example of how it can be used for cookies/sessions.