dongxuying7583 2018-04-26 17:44
浏览 66
已采纳

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

EDIT: To clarify, I am unable to extract the hashed password from my database using prepared statements.

I'm trying to create a login system that uses prepared statements, password_hash and password_verify.

I have created the registering form that creates the user, with the hashed password using password_hash($_POST['password'], PASSWORD_DEFAULT);

This works properly.

However, I am now stuck on creating the login form.

I am trying to get the password hash that gets stored when a user registers but I cannot get it to work with prepared statements.

This is what I currently have.

<?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();

  }
}

?>

How do I use the data that I got from the select query? And how do I use it to verify the password?

I tried:

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

That only stored the username, but not the password hash and I have no clue why.

Verifying the password would use this? password_verify($password, $hash);

UPDATE

<?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条回答 默认 最新

  • dsdvr06648 2018-04-26 18:19
    关注

    Read this PHP MANUAL.Try this:

     <?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
            }
    
          }
        }
    
        ?>
    

    This is how I created my login system:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试