简体   繁体   English

password_verify从数据库返回false,但从站点返回true?

[英]password_verify return false from database but true from site?

I need a bit of help here. 我需要一点帮助。 I don't really understands why these aren't both returning "true". 我真的不明白为什么它们都不都返回“ true”。 My test user with the id 6 has "test" as password. 我的ID为6的测试用户的密码为“ test”。 The value was stored when i created him. 该值是在我创建他时存储的。

I'm aware that the two hashed values aren't the same, but it seems like everyone is able to do it like that here on stackoverflow :) 我知道这两个哈希值并不相同,但是似乎每个人都可以在stackoverflow上做到这一点:)

<?php

require_once 'includes/database.php';
require_once 'includes/connection.php';

$sql_1 = 'SELECT * FROM anew_users WHERE user_id = 6';
$result_1 = $conn->query($sql_1);
while($row = $result_1->fetch_assoc()) {

    $password = $row["user_password"];

}

$submittedPassword = "test";

echo $password . "<br>";
echo $submittedPassword . "<br>";

$verify = password_verify($submittedPassword, $password);
echo var_dump($verify);

$password = hashPassword('test');
$submittedPassword = "test";

echo $password . "<br>";
echo $submittedPassword . "<br>";

$verify = password_verify($submittedPassword, $password);
echo var_dump($verify);

The output is: 输出为:

//OUTPUT
$2y$10$aAHYMEvpW2o9ZRsD0XN2XOpYz.dmuqj5v4UdAPIZX9Eo0SW0NjGRe
test
bool(false)

$2y$10$.srmofPee5SWV6nmOy0PAOIlzoJPT0SBnzNN0QkYZKlpk3LzgI7F.
test
bool(true)

I created the password with this code: 我使用以下代码创建了密码:

function hashPassword($string) {
    $output = password_hash($string, PASSWORD_DEFAULT, ['cost' => 10]);
    return $output;
}

$user_password = escape($_POST["user_password"]);
$user_passwordhashed = hashPassword($user_password);

Thanks in advance. 提前致谢。

UPDATE WITH ESCAPE FUNCTION 使用逃逸功能更新

function escape($string) {
    $string = trim($string);
    $string = stripslashes($string);
    $string = htmlspecialchars($string);
    return $string;     
}

The password that was hashed and stored in the database was not test , but was an empty string. 哈希并存储在数据库中的密码不是test ,而是一个空字符串。

$string = '';
$pass = '$2y$10$aAHYMEvpW2o9ZRsD0XN2XOpYz.dmuqj5v4UdAPIZX9Eo0SW0NjGRe';

$output = password_verify($string, $pass);
var_dump($output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM