我在让登录功能正常工作时遇到问题。即使我输入了错误的凭据,它也总是返回 true。谁能告诉我这是为什么?
这是功能
public function login($username, $password) {
global $pdo;
$users = new Users;
$user_id = $users->get_user_id($username);
$query = $pdo->prepare("SELECT * FROM `users` WHERE `username` = ? AND `password` = ?");
$query->bindValue(1, $username);
$query->bindValue(2, $password);
$query->execute();
if (!$query) {
return false;
}
return $user_id;
}
这里是它的实现位置。
if (isset($_SESSION['user_id']) === true) {
header('Location: backend.php');
exit();
} else {
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'All fields are required!';
} else {
$login = $users->login($username, $password);
if ($login === false) {
$errors[] = 'Username or password incorrect!';
} else {
$_SESSION['user_id'] = $login;
header('Location: backend.php');
exit();
}
}
}