Php 密码哈希返回的密码不匹配
我在注册过程中创建了一个密码\u散列,当我返回登录并在密码\u的帮助下键入电子邮件和密码时,验证它是否输出密码不匹配。为了检查我的查询是否有效,我已经在这个链接中使用在线工具生成了密码哈希 这允许用户成功登录,但密码自动生成的哈希值总是失败,这意味着我的查询没有问题。是否有一种解决方案可以使密码\u散列正常工作 任何帮助都是有用的。 提前感谢, 我尝试过使用静态密码散列,它看起来像$2y$10$3xrOcCz5fewSd90zS7dgseINs1WpD132MtrWjFmG3qSQcNDXpcgbe,它是Password@123 Ajax来处理注册Php 密码哈希返回的密码不匹配,php,password-hash,Php,Password Hash,我在注册过程中创建了一个密码\u散列,当我返回登录并在密码\u的帮助下键入电子邮件和密码时,验证它是否输出密码不匹配。为了检查我的查询是否有效,我已经在这个链接中使用在线工具生成了密码哈希 这允许用户成功登录,但密码自动生成的哈希值总是失败,这意味着我的查询没有问题。是否有一种解决方案可以使密码\u散列正常工作 任何帮助都是有用的。 提前感谢, 我尝试过使用静态密码散列,它看起来像$2y$10$3xrOcCz5fewSd90zS7dgseINs1WpD132MtrWjFmG3qSQcNDXpcg
$(function() {
$(".log").click(function() {
var email = $("#email").val();
var password = $("#password").val();
var confirm_password = $("#confirm_password").val();
var dataString = 'email='+ email + '&confirm=' + confirm;
if(email=='' ||password=='' || confirm_password=='' || (password !=confirm_password))
{
swal("Sorry !", "Couldn't create your account !", "error");
}
else
{
$.ajax({
type: "POST",
url: "join.php",
data: dataString,
success: function(){
//$('.success').fadeIn(200).show();
//$('.error').fadeOut(200).hide();
swal("Thanks !","Your account is created !"+" "+"Redirecting you to login","success");
setTimeout(function () {
window.location.href = "login.php"; //will redirect to your blog page (an ex: blog.html)
}, 2000);
}
});
}
return false;
});
});
</script>
$(函数(){
$(“.log”)。单击(函数(){
var email=$(“#email”).val();
var password=$(“#password”).val();
var confirm_password=$(“#confirm_password”).val();
var dataString='email='+email+'&confirm='+confirm;
如果(电子邮件=“”| |密码=“”| |确认|密码=“”| |(密码!=确认|密码))
{
swal(“对不起!”,“无法创建您的帐户!”,“错误”);
}
其他的
{
$.ajax({
类型:“POST”,
url:“join.php”,
数据:dataString,
成功:函数(){
//$('.success').fadeIn(200.show();
//$('.error').fadeOut(200.hide();
swal(“谢谢!”,“您的帐户已创建!”+“+”将您重定向到登录,“成功”);
setTimeout(函数(){
window.location.href=“login.php”;//将重定向到您的博客页面(例如:blog.html)
}, 2000);
}
});
}
返回false;
});
});
注册过程
require_once('dbconn.php');
if($_POST)
{
$email = $_POST['email'];
$cpassword = $_POST['confirm_password'];
$hashed = password_hash($cpassword, PASSWORD_DEFAULT);
$sql = "INSERT INTO user_login (user_email, user_password) VALUES (?,?)";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "ss", $email, $hashed);
$result = mysqli_stmt_execute($stmt);
if ($result) {
echo "Successfully Inserted !";
//echo '<script type="text/javascript">';
//echo 'setTimeout(function () { swal("Thanks!","Your account is created !","success");';
//echo '}, 1000);</script>';
}
else{
echo "Error occurred! ".mysqli_stmt_errno($stmt)." ".mysqli_stmt_error($stmt)." ";
$error = mysqli_stmt_error($stmt);
}
}
mysqli_close($conn);
?>
require_once('dbconn.php');
如果(美元邮政)
{
$email=$_POST['email'];
$cpassword=$\u POST['confirm\u password'];
$hashed=password\u hash($cpassword,password\u DEFAULT);
$sql=“插入用户登录(用户电子邮件、用户密码)值(?,)”;
$stmt=mysqli\u prepare($conn,$sql);
mysqli_stmt_bind_param($stmt,“ss”,$email,$hash);
$result=mysqli\u stmt\u execute($stmt);
如果($结果){
echo“已成功插入!”;
//回声';
//echo'setTimeout(函数(){swal(“谢谢!”,“您的帐户已创建!”,“成功”);
//回声'},1000);';
}
否则{
回显“发生错误!”。mysqli_stmt_errno($stmt)。“”。mysqli_stmt_Error($stmt)。“”;
$error=mysqli\u stmt\u error($stmt);
}
}
mysqli_close($conn);
?>
登录
如果(isset($\u POST['btn\u lg'])){
$email=$_POST['email'];
$password=$_POST['password'];
$sql=“从`user\u login`中选择`user\u email`,`user\u password`,其中`user\u email`=?”;
$stmt=mysqli\u prepare($conn,$sql);
mysqli_stmt_bind_param($stmt,“s”,$email);
$result=mysqli\u stmt\u execute($stmt);
mysqli_stmt_store_结果($stmt);
mysqli_stmt_bind_result($stmt,$email,$pass);
mysqli_stmt_fetch($stmt);
如果(mysqli_stmt_num_rows($stmt)==1)
{
//$row=mysqli\u stmt\u fetch($stmt);
$hash=$pass;
//echo$hash;
if(密码验证($password,$hash))
{
$\u会话['Logged']=true;
$\会话['email']=$email;
?>
window.location.href=“test.php”;
我希望登录结果为true,但输出为false数据库中密码列的长度是多少?是60个字符或更多?还是更少?从查询结果中得到什么输出?检查时,
$hash
的值是多少?为什么要将其重新分配给$hash
,为什么不直接使用$pass
在password\u verify call
中,如果看不到更多的输出,我真的无能为力。如果num\u rows==1,则只有三个逻辑结论1)在散列和存储密码之前密码被更改,2)在比较密码之前密码被更改,3)密码确实不正确。可能是pic可能会在某个地方增加一些空白?是否确实设置了$\u POST['btn\u lg']呢?是否设置了$\u POST['password']呢?我可以看到您在Ajax中传递confirm,但没有设置这两个字符的位置?另外,查看登录过程是如何工作的可能会很有用(例如,登录脚本的详细信息在哪里?我想你只是在这里显示了注册)“我没有为此使用任何ajax”不管怎样,我想说的是,您的表单提交方式可能有问题,因此在没有看到它的情况下我们不能说。关于空白注释,我不是指在哈希中,我是指在提交的密码字符串中(即post变量)-您是对的,这不太可能,但您正在正确使用密码\u验证,如果您确信您从select中获得了正确的用户详细信息,那么唯一的逻辑结论是,不知何故,密码字符串没有正确传递
if(isset($_POST['btn_lg'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "SELECT `user_email`, `user_password` FROM `user_login` WHERE `user_email` = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt,"s", $email);
$result = mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $email, $pass);
mysqli_stmt_fetch($stmt);
if(mysqli_stmt_num_rows($stmt) === 1)
{
//$row = mysqli_stmt_fetch($stmt);
$hash = $pass;
//echo $hash;
if(password_verify($password, $hash))
{
$_SESSION['Logged'] = true;
$_SESSION['email'] = $email;
?>
<script type="text/javascript">
window.location.href="test.php";
</script>
<?php
}
else
{
echo "The username or password do not match" ;
}
}
else
{
echo "Login Failed ! " .mysqli_stmt_errno($stmt)." ".mysqli_stmt_error($stmt)." ";
}
}
?>