【问题标题】:password_verify return false from database but true from site?password_verify 从数据库返回 false 但从站点返回 true?
【发布时间】:2015-10-12 09:41:14
【问题描述】:

我需要一些帮助。我真的不明白为什么这些都不返回“真实”。我的 id 为 6 的测试用户有“test”作为密码。这个值是在我创建他时存储的。

我知道这两个散列值不一样,但似乎每个人都可以在 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);

输出是:

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

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

我用这段代码创建了密码:

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

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

提前致谢。

使用转义函数更新

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

【问题讨论】:

  • 我们可以看看你的escape函数吗?
  • 我更新了我的帖子:)
  • 你确定用户id 6的密码真的是“test”吗?您可以尝试使用新密码更新该值吗?

标签: php password-protection password-encryption


【解决方案1】:

经过哈希处理并存储在数据库中的密码不是test,而是一个空字符串。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2012-06-29
    • 2013-02-26
    相关资源
    最近更新 更多