gpt4 book ai didi

PHP PDO登录系统失败

转载 作者:行者123 更新时间:2023-11-29 02:10:12 25 4
gpt4 key购买 nike

我正在制作一个登录系统,但似乎任何拥有任何密码的用户都可以进入我的系统。

我可以使用 mysqli 创建这个系统,但我遇到了 PDO 问题。我该如何解决。

if(isset($_POST["login-submit"])){
require "dbh.inc.php";
$username = $_POST["username"];
$user_password = $_POST["password"];
$user_password = password_hash($user_password, PASSWORD_DEFAULT);

$sql = "SELECT count(id) FROM users WHERE username = :username AND user_password = :user_password";
$stmt = $conn->prepare($sql);
$stmt->execute(["username" => $username, "user_password" => $user_password]);
if($stmt->rowCount() == 1){
session_start();
$_SESSION["username"] = $username;
header("location:../index.php?login=success");
exit();
} else {
header("location:../index.php?login=failed");
exit();
}
}

我已经测试了代码的所有其他部分,当我注册一个新用户时它们似乎工作得很好,但是如果我使用登录表单,其中任何有任何密码的人都可以进入我的系统。即使我输入了错误的用户和错误的密码,它也总是显示登录成功。我也看到其他关于堆栈溢出的问题与此类似,但这些代码有点复杂。

最佳答案

您再次对密码进行哈希处理以检查密码的有效性,您应该使用 password_verify 来检查密码是否合法。

if(isset($_POST["login-submit"])){
require "dbh.inc.php";
$username = $_POST["username"];
$user_password = $_POST["password"];

$sql = "SELECT * FROM users WHERE username = :username";
$stmt = $conn->prepare($sql);
$stmt->execute(["username" => $username]);
$user = $stmt->fetch();
$hash = $user['user_password'];
if(password_verify($user_password,$hash)){
session_start();
$_SESSION["username"] = $username;
header("location:../index.php?login=success");
exit();
} else {
header("location:../index.php?login=failed");
exit();
}
}

假设您使用 password_hash 函数将用户密码保存在数据库中。此外,在使用用户名获取后,获取哈希,然后使用 password_verify() 函数对其进行验证,

关于PHP PDO登录系统失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54753231/

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