无法验证PHP密码验证

无法验证PHP密码验证,php,passwords,password-hash,Php,Passwords,Password Hash,我正在做一些需要发送的项目。但现在我将坚持使用密码验证方法 //Login Existed User if (isset($_POST['login'])) { $email = mysqli_real_escape_string($con, $_POST['email']); $password = mysqli_real_escape_string($con, $_POST['password']); //$password = password_verify($

我正在做一些需要发送的项目。但现在我将坚持使用密码验证方法

//Login Existed User

if (isset($_POST['login'])) {
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $password = mysqli_real_escape_string($con, $_POST['password']);

    //$password = password_verify($con, $password);
    $cmmnd = "SELECT * FROM user WHERE email = '$email' AND password ='$password'";
    $run = mysqli_query($con, $cmmnd);

    if ($run -> num_rows > 0) {
        $data2 = $run -> fetch_array();
        if (password_verify($password, $data2['password'])) {
            header("Location: mainpage.php");
        }
    }else {
        echo "<script>alert('ID or Password Are Wrong!');
                                window.location= 'user.php'</script>";
    }
}

注册很好,但现在我无法登录。我真的很感谢你的帮助。在注册时,您已经为密码生成了一个哈希。但是在登录页面中,您将原始密码传递给select查询。

首先,您需要调整注册脚本。切换到prepared语句并将哈希插入数据库,而不是纯文本密码

if (isset($_POST['register'])) {
    $hash = password_hash($_POST['rpswd'], PASSWORD_DEFAULT);
    $stmt = $con->prepare('INSERT INTO `user` (`name`, `email`, `password`) VALUES (?,?,?)');
    $stmt->bind_param('sss', $_POST['fname'], $_POST['remail'], $hash);
    $stmt->execute();

    header("Location: regist.php");
    exit; // always add after header("Location: 
}
确保数据库中的密码字段为VARCHAR255,字符集为utf8mb4。还要确保重定向到regist.php的页面是正确的

然后,在登录页面上,您必须根据提供的电子邮件从数据库中获取单个字段密码。然后,您可以使用该值(如果存在)来检查密码\u验证。如果哈希匹配,则可以重定向。如果没有,则显示一条消息

//Login Existed User
if (isset($_POST['login'])) {
    $stmt = $con->prepare('SELECT password FROM user WHERE email = ?');
    $stmt->bind_param('s', $_POST['email']);
    $stmt->execute();
    $result = $stmt->get_result();

    $data2 = $result->fetch_array();
    if ($data2 && password_verify($_POST['password'], $data2['password'])) {
        header("Location: mainpage.php");
        exit;
    }

    // If the login was unssuccesful then display a message
    echo "<script>alert('ID or Password Are Wrong!');
                window.location= 'user.php'</script>";
}

在您的登录-删除和密码。。。部分,因为这是在密码验证中完成的。另外,请使用准备好的语句并删除对mysqli\u real\u escape\u string的调用!你的注册码错了。您对密码进行散列,但没有以任何方式存储或使用散列。您正在数据库中保存原始纯文本密码。您应该存储$hash,而不是$rpswd。即使没有准备好的语句,也永远不需要在散列之前转义密码,因为您只存储散列。
//Login Existed User
if (isset($_POST['login'])) {
    $stmt = $con->prepare('SELECT password FROM user WHERE email = ?');
    $stmt->bind_param('s', $_POST['email']);
    $stmt->execute();
    $result = $stmt->get_result();

    $data2 = $result->fetch_array();
    if ($data2 && password_verify($_POST['password'], $data2['password'])) {
        header("Location: mainpage.php");
        exit;
    }

    // If the login was unssuccesful then display a message
    echo "<script>alert('ID or Password Are Wrong!');
                window.location= 'user.php'</script>";
}