[英]PHP Verifying passwords using password_verify and password_hash
我尝试使用PHP的哈希值对我保存到数据库的密码进行加密。 这是我的登录代码:
$email=$_POST['email'];
$password=$_POST['password'];
$sql ="SELECT user_email FROM tbl_user WHERE user_email=:email ";
$query= $dbh -> prepare($sql);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 0)
{
if(password_verify($password, $results["user_password"]))
{
$_SESSION['signin']=$_POST['email'];
$currentpage=$_SERVER['REQUEST_URI'];
echo "<script type='text/javascript'> document.location = '$currentpage'; </script>";
}
else
{
echo "<script>alert('Invalid Details');</script>";
}
} else{
echo "<script>alert('Invalid Details');</script>";
}
这是我的注册代码:
$user_fullname=$_POST['fullname'];
$user_email=$_POST['email'];
$user_phonenumber=$_POST['telephone'];
$hashToStoreInDb = password_hash($_POST['password'], PASSWORD_DEFAULT);
$sql="INSERT INTO tbl_user(user_email,user_fullname,user_phonenumber,user_password) VALUES(:user_email,:user_fullname,:user_phonenumber,:hashToStoreInDb)";
$query = $dbh->prepare($sql);
$query->bindParam(':user_email',$user_email,PDO::PARAM_STR);
$query->bindParam(':user_phonenumber',$user_phonenumber,PDO::PARAM_STR);
$query->bindParam(':user_fullname',$user_fullname,PDO::PARAM_STR);
$query->bindParam(':hashToStoreInDb',$hashToStoreInDb,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId)
{
echo "<script>alert('Something went wrong. Please try again');</script>";
}
else
{
echo "<script>alert('Registration successfully. Now you can Sign In');</script>";
}
我可以注册并将数据保存在我的数据库中,注册后我不需要验证登录密码,而我却无法登录。 如果我输入了错误的代码或任何建议代码来修改我的代码,请帮助我。 感谢帮助
您的登录查询未检索存储在数据库中的密码哈希。 您需要获取数据库上的密码哈希,并将其与登录时提交的用户密码生成的密码哈希进行比较。我认为这可以解决问题。
将您的登录查询更改为:
$sql ="SELECT user_email, user_password FROM tbl_user WHERE user_email=:email ";
同样,您的查询将作为对象返回。 因此,您需要引用$results->user_password
类的变量,而不是引用$results["user_password"]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.