Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP密码\u验证假阴性_Php_Mysqli_Passwords_Php 7.1 - Fatal编程技术网

PHP密码\u验证假阴性

PHP密码\u验证假阴性,php,mysqli,passwords,php-7.1,Php,Mysqli,Passwords,Php 7.1,背景- 我花了一个小时左右的时间浏览了各种帖子、网站等,想知道为什么我的密码检查功能突然停止了工作 每次我尝试使用它时,它都返回false,包括输入的密码是否正确 我已经回显了我的输入和散列,通过将表中的散列与回显的散列进行比较,这两个散列匹配得很好 但是,当这些变量通过password\u verify输入时,它会100%返回false 我认为有什么不对基于,我认为这是由于hashedPassword变量的解析方式造成的。当is将两者进行比较时,变量类型出现错误并返回false 我的代码和数据

背景- 我花了一个小时左右的时间浏览了各种帖子、网站等,想知道为什么我的密码检查功能突然停止了工作

每次我尝试使用它时,它都返回false,包括输入的密码是否正确

我已经回显了我的输入和散列,通过将表中的散列与回显的散列进行比较,这两个散列匹配得很好

但是,当这些变量通过password\u verify输入时,它会100%返回false

我认为有什么不对基于,我认为这是由于hashedPassword变量的解析方式造成的。当is将两者进行比较时,变量类型出现错误并返回false

我的代码和数据库是什么样子的-

用户表。包含其他列

userId | username | password
----------------------------
  1    | example  |  temp1
类,该类包含密码检查器函数。已修剪掉尝试/捕捉

public function checkPasswordCorrect($username, $password) {  // Returns true is the entered password is correct and false if it isn't.

   // This creates a mysqli context to run the connection through.
   $mysqli = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbPlusPlus);

   // If there is an error connecting to the database, it will be printed and the process will close.
   if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
   }

   $sql = "SELECT password FROM users WHERE username='$username'";
   $result = $mysqli->query($sql);
   $row = $result->fetch_array(MYSQLI_ASSOC);

   $hashedPassword = $row["password"];

   $mysqli->close();

   echo $password."<br>".$hashedPassword."<br>"; // The input is correct and the hash matches that in the table.

   // We return a value of true or false, depending on the outcome of the verification.
   if(password_verify($password, $hashedPassword)) {
       echo "return true";
       return true;
   }          
   else {
       echo "return false";
       return false;
   }
}
我在问什么-

是否有更好的方法来检查密码输入,更具体地说,我从表中提取信息的方法,必须有更简单的方法

变量解析是罪魁祸首吗?有没有关于这个主题的文档或文章我没有看过

我知道什么我对mysqli的使用令人震惊,我是新手。我也知道有类似的答案,但似乎更多的是由于某种语法错误

感谢您抽出时间来帮忙!:D

对于所有担心我会受到SQL注入攻击的人:我已经修改了这个示例代码 我大量使用事先准备好的陈述。谢谢你的支持 关注:


检查您的表结构。要保存整个散列,密码\u BCRYPT的散列字段长度必须为60,密码\u DEFAULT的散列字段长度必须为255。您从数据库中获得的数据长度为18个字符。您可能将其命名为VARCHAR18,因此它在保存时会被截断,这就是您无法获得匹配项的原因


编辑:为密码\默认算法添加长度。谢谢@Don'tPanic。

数据库中的密码字段允许有多少个字符?它打印的密码太短,已被截断。使用password_verity生成的密码哈希长度约为60个字符。“如果你有一个较短的varchar字段,则不会保存完整的散列。我觉得讽刺的是,你对密码进行散列,但不试图阻止,就像你只希望系统的一半是安全的,”他说。了解。即使是这样也不安全!我推荐PDO,我希望它比使用非参数化查询更简单、更干净、更安全。可以肯定的是,散列密码是用password_hash创建的吗?varchar18永远不能使用password_hash,因为它生成的散列长度超过18个字符。将其更改为至少60,以后可能会更长,再次散列密码现有密码无法保存,然后重试。建议长度为255。@Don't我将在中编辑它,沿BCRYPT长度记录它。谢谢我会从你的答案中删除所有的问题,答案应该是答案,而不是问题。这更像是一个修辞性的问题。我在安威把它删掉了。谢谢
temp0022
$2y$10$9TgjJzSaqOB
return false