我正在尝试在 MySQL 中存储一个加密的密码,至于注册部分,它可以正常工作,但是当我尝试进行登录时,一切都变糟了。
我无法根据存储在 MySQL 中的哈希值验证 $_POST['password']。 我不知道我做错了什么。
这是我的 register.php,它可以正常工作:
register.php(工作)
$post_password = mysqli_real_escape_string($_POST['password']);
$password_hash = password_hash($post_password, PASSWORD_BCRYPT);
mysqli_query goes here...
login.php(不工作)
$con = mysqli_connect("XXX","XXX","XXX","XXX");
$post_username = mysqli_real_escape_string($con,$_POST['username']);
$post_password = mysqli_real_escape_string($con,$_POST['password']);
// Getting the stored Hash Password from MySQL
$getHash = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM anvandare WHERE username = '$post_username'"));
$got_Hash = $getHash['password'];
// Checking if what the user typed in matches the Hash stored in MySQL
// **This is where it all goes wrong**
if (password_verify($post_password, $got_Hash)) {
echo "The posted password matches the hashed one";
}
else {
echo "The posted password does not match the hashed one";
}
当我运行上面的代码时,我得到了“正确的密码”消息只需输入用户名并保留密码字段。
我错过了什么?
最佳答案
实际上,您需要确保在密码栏中允许超过 100 个字符,以便所有散列密码都可以保存在该字段中。这也发生在我身上,脚本是正确的,一切正常,但我犯的唯一错误是我在密码字段中不允许超过 40 个字符,这是最大的错误。将最大限制从 40 增加到 100 后,一切正常:)
关于php - 使用 password_verify 和 MySQL 验证密码哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28318885/