Php 为什么不存在的用户可以使用此脚本登录?

Php 为什么不存在的用户可以使用此脚本登录?,php,authentication,pdo,passwords,prepared-statement,Php,Authentication,Pdo,Passwords,Prepared Statement,我有一些PDO: if (empty($this->user->username) || empty($this->user->password)) throw new Exception("Error Processing Request", 1); include('dbconnect.php'); // Normally I'd store the db connect script outside of webroot $pdo = new P

我有一些PDO:

if (empty($this->user->username) || empty($this->user->password))
    throw new Exception("Error Processing Request", 1);
include('dbconnect.php');       // Normally I'd store the db connect script outside of webroot
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
$stmt = $pdo->prepare('SELECT userFName FROM Users WHERE username = ? AND password = ? AND roleID = 1');
$stmt->bindParam(1, $this->user->username);
$stmt->bindParam(2, $this->user->password);
$stmt->bindColumn(1, $userFName, PDO::PARAM_STR);
try {
    $stmt->execute();
}
catch (PDOException $e) {
    echo 'Invalid username or password: ' . $e->getMessage();
}
$stmt->fetch(PDO::FETCH_BOUND);
$this->user->firstName = $userFName;
如果发布的用户名和/或密码错误,仍然会发生登录。我认为PDO有问题;当我试图对照数据库中的记录检查$this->user->password时,是否遗漏了一些关键信息

(顺便说一句,我正在使用加密)

如果没有结果,则不会引发异常。您需要检查查询结果是否为用户返回了一行

编辑:


为什么不省略本节上面的
和roleID=1
?检查和重定向到登录页面的行在哪里。不过我把它修好了:)嗯。我忘记了rowCount()技巧(如果您还不知道的话):
$res=$stmt->fetchAll(PDO::FETCH_ASSOC)
(或
FETCH(PDO::FETCH_ASSOC)
)将返回包含从数据库收集的所有数据的关联数组,并将其放入
$res
。我认为这是访问查询结果更简单的方法。
$stmt->execute();
$stmt->execute();
if ($stmt->rowCount() > 0) {
    //user found
} else {
    //user not found
}