【问题标题】:password_verify() not verifying hashed passwordpassword_verify() 不验证散列密码
【发布时间】:2016-05-22 18:25:28
【问题描述】:

我正在尝试使用password_hashpassword_verify 在PHP 中设置密码。我正确地散列了密码,因为它正在散列到数据库中,但是当我尝试在登录时取消散列密码时,它似乎不想工作。密码是从 Android 应用程序接收的,但在回显用户名和密码后,它们应该是正确的。为了散列密码,我使用PASSWORD_DEFAULT 作为散列技术。

代码:

<?php
error_reporting(0);
require_once('dbconnect.php');

    $username = $_POST["username"];
    $password = $_POST["password"];

    $result = $conn->query("SELECT * FROM User WHERE username ='$username'");

        if(empty($result)){
            die("Username doesn't exist");
        }   

    $dbpass = $conn->query("SELECT password FROM User WHERE username = '$username'");

    if (password_verify($password, $dbpass)){

        $stmt = "SELECT * FROM User WHERE username='$username' and password='$password'";

        $check = mysqli_fetch_array(mysqli_query($conn, $stmt));

        if(isset($check)){
            echo "success";
        }else{
            echo "Invalid Username or Password";
        }
    }
    else {
        echo "password not unhashing";
    }

$conn->close(); 

我是否遗漏了一些明显的东西?

【问题讨论】:

  • $dbpass 不是密码,而是查询的结果集;您将需要获取单个行,然后从该行访问密码条目..... PHP 数据库 101
  • 您似乎也在使用数据库访问的程序和对象样式进行选择
  • $dbpass = $conn->query("SELECT password FROM User WHERE username = '$username'"); if (password_verify($password, $dbpass)){ --- 你需要从 dbPass 获取密码
  • 该死的,谢谢大家,我知道为什么我现在变得愚蠢了。
  • 我将用户名设置为'; drop table users; --' ;)

标签: php hash


【解决方案1】:

首先,使用准备好的语句来消除 SQL 注入的威胁,否则您的登录屏幕将成为攻击媒介。那么问题是你没有得到实际的 dbpass,你得到了一个包含 $dbpass 的结果集,而没有取消引用它。

试试这个方法:

//username in where clause is coming from the user, don't execute it
//also fetch a clean copy of the username from the database we can trust to do things with like display -- assuming we filtered it on the way into the database.    
$stmnt = $conn->prepare('select username,password from user where username = ?') or die('...');


//username must be a string, and to keep it clear it came from a user, and we don't trust it, leave it in POST.
$stmnt->bind_param('s',$_POST['username']) or die('...');


//Do the query.
$stmnt->execute() or die('...');

//Where to put the results.
$stmnt->bind_result($username,$dbpass);

//Fetch the results
if($stmnt->fetch()) //get the result of the query.
{
  if(password_verify($_POST['password'],$dbpass))
  {
    //The password matches.
  }
  else
  {
    //password doesn't match.
  }
}
else
{
  //username is wrong.
}

【讨论】:

    猜你喜欢
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2020-08-16
    • 2016-05-10
    • 2020-02-29
    • 2019-05-24
    相关资源
    最近更新 更多