无法使用password_verify

时间:2018-07-26 16:16:50

标签: php mysql passwords

我正在尝试建立一个注册/登录系统。散列的密码已成功保存到数据库中,但是当我尝试登录时,它会显示“无效登录”,这意味着它不会验证密码。帮帮我,这是我第一次使用密码哈希并验证

Signup.php

<?php
include('AdminPanel/connect.php');

$name = $_POST['txt_name'];
$email = $_POST['txt_email'];
$password = password_hash($_POST['txt_pass'], PASSWORD_DEFAULT);
$radioVal = $_POST['Gender'];


if($radioVal == "Male")
{
    $radioVal = "Male";
}
else if ($radioVal == "Female")
{
    $radioVal = "Female";
}


$queryget = mysqli_query($con,"SELECT Email FROM signup WHERE Email='$email'") or die ("Query didnt work");
    $row = mysqli_fetch_array($queryget);


    $emaildb = $row['Email'];
    if($emaildb!=$email){
        echo"success";
        $insert = mysqli_query($con,"insert into signup (Name,Email,Password,Gender) values ('$name','$email','$password','$radioVal')");
    }else{
echo"Email already exists";
}

?>

Login.php

<?php

include('AdminPanel/connect.php');

session_start();

$email = $_POST['txt_email'];
$password = $_POST['txt_pass'];
$info = mysqli_query($con,"select count(*) from signup where Email = '$email' and Password = '$password'");

$row = mysqli_fetch_array($info);
if (($row[0] > 0) && password_verify($password, $row['Password']))
{

    $_SESSION['txt_email']=$email;
    echo "success";

}
else
{
    echo "Invalid login<br>Please re-enter your credentials";
}

?>

1 个答案:

答案 0 :(得分:2)

您正在选择count(*)

$info = mysqli_query(
    $con, "select count(*) from signup where Email = '$email' and Password = '$password'"
);

但随后引用一个字段:

$row['Password']

您需要选择(至少)该字段,但要保留密码条件,因为您获得的密码与数据库中的密码不匹配:

$info = mysqli_query(
    $con, "select * from signup where Email = '$email'"
);

也不要这样做,因为SQL注入。