无法使用预准备语句从数据库中提取密码哈希

时间:2018-04-26 17:44:10

标签: php mysql prepared-statement

编辑:为了澄清,我无法使用预准备语句从我的数据库中提取散列密码。

我正在尝试创建一个使用预准备语句,password_hash和password_verify的登录系统。

我创建了使用password_hash($_POST['password'], PASSWORD_DEFAULT);

创建用户的注册表单和散列密码

这很正常。

但是,我现在仍然在创建登录表单。

我正在尝试获取用户注册时存储的密码哈希,但我无法使用预处理语句。

这就是我现在所拥有的。

<?php
require('db.php');

if(isset($_POST['submit'])) {
  $stmt = $connect->prepare('SELECT user_name, user_password FROM `users` WHERE user_name = ?');

  if($stmt) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt->bind_param('s', $username);

    $stmt->execute();

  }
}

?>

如何使用我从select查询中获得的数据?我如何使用它来验证密码?

我试过了:

$stmt->store_result();
$stmt->bind_result($loginUsername, $hash);

那只存储了用户名,而不是密码哈希,我不知道为什么。

验证密码会使用此功能吗? password_verify($password, $hash);

更新

<?php
require('db.php');

if(isset($_POST['submit'])) {
  $stmt = $connect->prepare('SELECT user_name, user_password FROM `users` WHERE user_name = ?');

  if($stmt) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt->bind_param('s', $username);

    $stmt->execute();

    // Get query results
    $result = $stmt->get_result();

    // Fetch the query results in a row
    while($row = $result->fetch_assoc()) {
      $hash = $row['user_password'];
      $username = $row['user_name'];
    }

    // Verify user's password $password being input and $hash being the stored hash
    if(password_verify($password, $hash)) {
      // Password is correct
    } else {
      // Password is incorrect
    }

  }
}

?>

1 个答案:

答案 0 :(得分:1)

阅读此PHP MANUAL。试试这个:

 <?php
    require('db.php');

    if(isset($_POST['submit'])) {
      $stmt = $connect->prepare('SELECT user_name, user_password FROM `users` WHERE user_name = ?');

      if($stmt) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $stmt->bind_param('s', $username);

        $stmt->execute();

        // Get query results
        $stmt->bind_result($user_name,$hash);
        $stmt->store_result();

        // Fetch the query results in a row
        $stmt->fetch();

        // Verify user's password $password being input and $hash being the stored hash
        if(password_verify($password, $hash)) {
          // Password is correct
        } else {
          // Password is incorrect
        }

      }
    }

    ?>

这就是我创建登录系统的方式:

$stmt = mysqli_prepare($link,"SELECT user_email,user_password,user_firstname,user_lastname,user_role,username FROM users WHERE user_email=?");
        mysqli_stmt_bind_param($stmt,"s",$email);
        mysqli_stmt_execute($stmt);
        confirmQuery($stmt);
        mysqli_stmt_bind_result($stmt,$user_email,$user_password,$user_firstname,$user_lastname,$user_role,$username);
        mysqli_stmt_store_result($stmt);
        mysqli_stmt_fetch($stmt);
        if(mysqli_stmt_num_rows($stmt) == 0)
            relocate("../index.php?auth_error=l1");
        else{
            if(password_verify($password,$user_password)){
                $_SESSION['userid'] = $user_email;
                $_SESSION['username'] = $username;
                $_SESSION['firstname'] = $user_firstname;
                $_SESSION['lastname'] = $user_lastname;
                $_SESSION['role'] = $user_role;
                if(isset($_POST['stay-logged-in']))
                    setcookie("userid", $email, time() + (86400 * 30), "/");
                relocate("../index.php?auth_success=1");
            }else
                relocate("../index.php?auth_error=l2");
        }
        mysqli_stmt_close($stmt);