gpt4 book ai didi

php - 即使提供了错误的用户名和密码,PHP登录脚本也会重定向

转载 作者:行者123 更新时间:2023-12-04 13:26:45 26 4
gpt4 key购买 nike

那是代码

<?php

//Start session
session_start();

//Then we retrieve the posted values for user and password.
$username = $_POST['username'];
$password = $_POST['password'];

//Users defined in a SQLite database
$db = new PDO("sqlite:/www/test.db");
$result = $db->query("SELECT COUNT(*) FROM users WHERE Username = '$username' AND Password = '$password'");

if ($result > 0)
{
//If user and pass match any of the defined users
$_SESSION['loggedin'] = true;
// close the database connection
unset($db);
header("Location: index.php");
};

//If the session variable is not true, exit to exit page.
if(!$_SESSION['loggedin'])
{
// close the database connection
unset($db);
header("Location: login.html");
exit;
};

?>


数据库架构:


用户名TEXT NOT NULL主键唯一,密码TEXT


唯一的行数为Username ='admin'和Password ='admin'

有什么想法为什么即使用户名和密码不在数据库中,脚本仍每次将我重定向到index.php?

提前致谢

最佳答案

如果没有遇到错误,$db->query将返回资源。不是查询的结果。因此,由于您的查询执行得很好,因此您将获得一个始终大于0的资源句柄。这就是每个人似乎都能成功登录的原因。

您需要获取查询结果,并检查COUNT(*)的值是否大于零(或等于1)。

关于php - 即使提供了错误的用户名和密码,PHP登录脚本也会重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10285749/

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