使用password_verify和MySQL验证密码哈希

时间:2015-02-04 10:21:56

标签: php

我试图在MySQL中存储一个加密的密码,对于注册部分,它可以正常工作,当我尝试登录的时候,它会向南移动。

我无法针对存储在MySQL中的哈希验证$ _POST ['密码']。 我不知道我做错了什么。

这是我的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";
}

当我运行上面的代码时,我得到了#34;正确的密码"消息只需输入用户名并保留密码字段。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

实际上,您需要确保在密码列中允许超过100个字符,以便可以在字段中保存所有哈希密码。这也发生在我身上,脚本是正确的,一切正常,但我唯一的错误就是我在密码字段中不允许超过40个字符,这是最大的错误。将最大限制从40增加到100后,一切正常:)

相关问题