php password_hash 和 password_verify 问题不匹配
我正在尝试 PHP 5.5 中的一个名为 password_hash() 的新函数.
I am trying out a new function from PHP 5.5 called password_hash().
无论我做什么,$hash 和 $password 都不会匹配.
No matter what i do the $hash and the $password wont match.
$password = "test";
$hash = "$2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e";
if (password_verify($password, $hash)) {
echo "Success";
}
else {
echo "Error";
}
推荐答案
你的代码的问题是你使用双引号 "
而不是单引号 '
处理哈希时.
The problem with your code is that you are using the double quotation marks "
instead of the single quotation marks '
when dealing with your hash.
分配时:
$hash = "$2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e";
这让 php 认为你有一个名为 $2y
的变量和另一个名为 $10
的变量,最后是第三个名为 $fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e
的变量.显然不是这样的.
It's making php think you have a variable called $2y
and another one called $10
and finally a third one called $fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e
. Which obviously isn't the case.
我在打开错误报告时注意到错误:
I noticed when turning on error reporting that the error:
注意:未定义变量:fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e
Notice: Undefined variable: fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e
被 PHP 抛出.
用单引号替换你所有的双引号来修复.
Replace all your double quote marks with single quote marks to fix.
例如
$hash = '$2y$10$fXJEsC0zWAR2tDrmlJgSaecbKyiEOK9GDCRKDReYM8gH2bG2mbO4e';
将整个哈希视为文字字符串,而不是带有嵌入变量的字符串.
Treats the whole hash as a literal string instead of a string with embedded variables.
相关文章