duan0424 2016-10-19 10:47
浏览 42
已采纳

无法使用password_verify函数验证[复制]

This question already has an answer here:

I am having some problems with verifying password when users are logging in to my application - this is a work in progress and I am fully aware that the security isn't complete and is open to attack but it isn't published I am testing at the moment.

I have hashed the password using: password_hash($password, PASSWORD_DEFAULT);

I am now looking to verify this but my script seems to fail and I know where but am not sure what I need to change in order for the verification to happen as I have changed from procedural to OOP.

$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);


$query = "SELECT * FROM users WHERE (email = '". $email ."') AND password = '". $password ."'" ;
$result = $con->query($query);

if ($result->num_rows === 1) {
    $row = $result->fetch_array(MYSQLI_ASSOC);
    if (password_verify($password, $row['password'])) {

            $_SESSION['email'] = $email;
            $_SESSION['id'] = $id;
                $row = mysqli_fetch_assoc($result);
            $_SESSION['email'] = $row['email'];
            $_SESSION['id'] = $row['id'];

            header('Location: ../dashboard/index');

            }else{
                header('Location: ../index.php?message=Email address or password is incorrect');
            }

}

I know that the script is failing inside of the if statement where the verification happens as it serves me a blank page on the login script file (above). I simply echoed out 'hi' to test and it is the session scrip that seems to be the issue here.

</div>
  • 写回答

1条回答 默认 最新

  • douxigai8757 2016-10-19 10:53
    关注

    Change these two lines and it should work.

    $password = mysqli_real_escape_string($con, $_POST['password']);
    $password = $_POST['password'];
    
    $query = "SELECT * FROM users WHERE (email = '". $email ."') AND password = '". $password ."'" ;
    $query = "SELECT * FROM users WHERE (email = '". $email ."')" ;
    

    You will not be able to select the password from the one submitted via the post as the stored one is hashed.

    You should only have a single entry in your table per email, however you could check that the number of rows returned is equal to one if you wanted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置