gpt4 book ai didi

php - 在 PDO 准备好的语句中验证 password_hash()

转载 作者:可可西里 更新时间:2023-11-01 08:15:45 26 4
gpt4 key购买 nike

我正在尝试使用 bcrypt 算法对密码进行哈希处理,但我遇到了几个问题。首先,我找不到合适的位置来检查 password_verify() 是否返回 true。

$admin = $_POST['admin-user'];
$pass = $_POST['admin-pass'];

$password_hash = password_hash($pass, PASSWORD_BCRYPT);

if (isset($admin)&&isset($pass)&&!empty($admin)&&!empty($pass)) {

$admin_select = $link->prepare("SELECT `id` FROM `admins` WHERE `username` = :admin");

$admin_passwd = $link->prepare("SELECT `password` FROM `admins` WHERE `username` = :admin_pw");
$admin_passwd->execute(array(':admin_pw' => $admin));
$admin_pwd = $admin_passwd->fetch(PDO::FETCH_ASSOC);

if (password_verify($pass, $admin_pwd)){

if ($admin_select->execute(array(':admin' => $admin))) {
$res = $link->query('SELECT COUNT(*) FROM requests');
$query_num_rowz = $res->fetchColumn();
if ($query_num_rowz == 0) {
echo 'No records found';
} else if ($query_num_rowz > 0) {
$query = $link->prepare("SELECT id FROM admins WHERE username = :admin");
$query->execute(array(':admin' => $admin));
$admin_id = $query->fetch(PDO::FETCH_ASSOC);
$_SESSION['admin_id'] = $admin_id;
header('Location: index.php');
}
}
}
}

其次,我不确定这是选择用户密码的正确方法。

$admin_passwd = $link->prepare("SELECT `password` FROM `admins` WHERE `username` = :admin_pw");
$admin_passwd->execute(array(':admin_pw' => $admin));
$admin_pwd = $admin_passwd->fetch(PDO::FETCH_ASSOC);

最佳答案

由于您没有将 ->fetch 放入循环中,因此单次调用将返回一行关联数组。您必须首先访问正确的索引(在本例中为 password)。然后将 password_verify 中的行值(至少如果它已经被散列)与用户输入进行比较。粗略的例子:

if(!empty($_POST['admin-user'] && !empty($_POST['admin-pass']))) {
$admin = $_POST['admin-user'];
$pass = $_POST['admin-pass'];

$admin_info = $link->prepare("SELECT `password` FROM `admins` WHERE `username` = :admin_user");
$admin_info->execute(array(':admin_user' => $admin));
$row = $admin_info->fetch(PDO::FETCH_ASSOC);

if($row && password_verify($pass, $row['password'])) {
// okay
} else {
// no such user/password
}
}

关于php - 在 PDO 准备好的语句中验证 password_hash(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29034808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com