为什么password_verify返回false?
我正在使用 password_verify
来检查我的哈希密码。我有 PHP 5.5:
// get result row (as an object)
$result_row = $result_of_login_check->fetch_object();
// using PHP 5.5's password_verify() function to check if the provided password fits
// the hash of that user's password
if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {
// ...
}
我在 password_verify
上得到 false
。我已经检查过 posts 值和 mysql user_password_hash 返回。
我不知道为什么它返回 false。
有什么想法吗?
I'm using password_verify
to check my hashed password. I have PHP 5.5:
// get result row (as an object)
$result_row = $result_of_login_check->fetch_object();
// using PHP 5.5's password_verify() function to check if the provided password fits
// the hash of that user's password
if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {
// ...
}
I'm getting false
on password_verify
. I've already checked the posts value and mysql user_password_hash return.
I don't know why it's returning false.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据手册,问题可能出在您的列长度上:
建议将结果存储在可扩展超过 60 个字符的数据库列中(255 个字符是一个不错的选择)。 链接
Probably the problem is with your column length, from the manual:
it is recommended to store the result in a database column that can expand beyond 60 characters (255 characters would be a good choice). link
password_verify
有多种原因可能会返回 false,它的范围可能从表的设置到密码的实际比较,以下是失败的常见原因。列设置
表中密码列的长度太短:
PASSWORD_DEFAULT
,则建议将结果存储在可扩展至 60 个字符以上的数据库列中(255 个字符是一个不错的选择)。PASSWORD_BCRYPT
,则建议将结果存储在 60 个字符的数据库列中,因为PASSWORD_BCRYPT
始终会生成 60 个字符的字符串或 FALSE失败时。密码清理
另一个常见原因是,当开发人员尝试“清理”用户密码以防止其被恶意攻击时,结果会导致输入与表中存储的内容不同。甚至没有必要转义输入,您应该使用准备好的语句。您甚至不应该
修剪
密码,因为这可能会更改最初提供的密码。密码验证
使用
password_verify
时,您需要将明文密码与数据库中的哈希值进行比较,而不是比较哈希值(这里的含义是您需要在用户注册时存储用户的哈希密码) :Repl
硬编码密码
在您使用硬编码哈希且面临的情况下问题,请确保在变量中存储值时使用单引号而不是双引号,因为使用双引号时将解释
$
: it/repls/DarkblueFractalOperatingenvironment" rel="noreferrer">Repl - 分别注释掉。
附录
根据文档:
There are a variety of reasons why
password_verify
could be returning false, it can range from the setup of your table to the actual comparing of the password, below are the common causes of it failing.Column Setup
The length of the password column in your table is too short:
PASSWORD_DEFAULT
then it is recommended to store the result in a database column that can expand beyond 60 characters (255 characters would be a good choice).PASSWORD_BCRYPT
then it is recommended to store the result in a database column that is 60 characters becausePASSWORD_BCRYPT
will always result in a 60 character string or FALSE on failure.Password Sanitization
Another common cause is when developers try to "clean" the user's password to prevent it from being malicious, as a result, this causes the input to be different to what is being stored in the table. It is not even necessary to escape the input, you should use prepared statements instead. You shouldn't even
trim
the passwords as that could change that which was originally provided.Password Verification
When using
password_verify
you need to compare the plaintext password with the hash from the database, not compare hashes (the implication here being that you need to have stored the hashed password of the user when they register):Repl
Hardcoded Passwords
In the instance that you are using a hardcoded hash and you are facing issues, ensure that you are using single quotes instead of double quotes when storing the value in the variable as the
$
will be interpreted in when using double quotes:Repl - Comment out respectively.
Addendum
As per the documentation:
调试问题的简单步骤:
在注册阶段打印并写下原始密码和哈希值的准确值。类似的东西
然后将输出复制并粘贴到某个文本文件中。
打印并写下登录阶段提供的密码的准确值以及从数据库返回的哈希值。类似的东西
然后简单地比较这些值 如果
password_verify()
失败,则原始密码或哈希值不匹配。如果原始密码不匹配,则输入的密码要么不正确,要么在一些无用的“清理”过程中被破坏。如果从数据库返回的哈希不匹配,那么它要么在一些无用的“清理”过程中被损坏,要么由于数据库中不正确的列属性而被更改。Simple steps to debug the issue:
Print out and write down exact values of the raw password and the hash during the registration phase. Something like
then copy and paste the output into some text file.
Print out and write down exact values of the password provided during login phase and the hash returned from DB. Something like
And then simply compare those values. If
password_verify()
fails, either raw passwords or hashes don't match. If raw passwords don't match then the entered password is either incorrect or was maimed during some useless "sanitization". If the hash returned from DB doesn't match, then it either was maimed during some useless "sanitization" or it was changed due to incorrect column properties in the database.检查参数顺序
只是指出发生在我身上的事情,几分钟前我正在阅读同一主题并试图解决我的问题,并且也可以帮助您解决问题。
看看您是否以正确的顺序为password_verify()提供参数:
password_verify(字符串 $password, 字符串 $hash)
密码是您通过邮寄或其他方式收到并正在尝试验证的密码。
哈希是您要与密码进行比较的哈希值。(例如,存储在数据库中的密码哈希值)
我碰巧颠倒了参数的顺序,所以结果没有按我的预期出现。
Check the parameters order
Just to point something that happened to me, who was reading this same topic trying to solve my problem minutes ago, and can help you to solve the problem as well.
See if you are giving the parameters for password_verify() in the right order:
password_verify(string $password, string $hash)
password is the password you received by post or other method and are trying to validate.
hash is the hash that you want to compare the password to.(e.g. The password hash stored in your Data Base)
It happened to me that I inverted the order of the parameters, so the result wasn't coming as I expected to.