需要 PHP PDO 帮助
我试图获取刚刚登录的用户的 UserID 并将其存储在 $_SESSION['UserID'] 中,但它没有这样做。
不存储它。 数组 ( [LoggedIn] => 1 [UserID] => )
// Database Connection File
require("../system/config.php");
$user['username'] = $_POST['username'];
$user['password'] = md5($_POST['password']);
$login_check = $DBH->prepare("SELECT Username, Password FROM Users WHERE Username = ? AND Password = ?");
$login_check->execute(array($user['username'], $user['password']));
if(count($login_check) == 1) {
session_start();
$row = $login_check->fetch();
$_SESSION['LoggedIn'] = 1;
$_SESSION['UserID'] = $row['UserID'];
print_r($_SESSION);
}
else {
echo "You have entered an incorrect Username and Password, please try again.";
}
I am trying to fetch the UserID of the user that has just logged in and store it in $_SESSION['UserID'], however it is not doing so.
Doesn't store it. Array ( [LoggedIn] => 1 [UserID] => )
// Database Connection File
require("../system/config.php");
$user['username'] = $_POST['username'];
$user['password'] = md5($_POST['password']);
$login_check = $DBH->prepare("SELECT Username, Password FROM Users WHERE Username = ? AND Password = ?");
$login_check->execute(array($user['username'], $user['password']));
if(count($login_check) == 1) {
session_start();
$row = $login_check->fetch();
$_SESSION['LoggedIn'] = 1;
$_SESSION['UserID'] = $row['UserID'];
print_r($_SESSION);
}
else {
echo "You have entered an incorrect Username and Password, please try again.";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:您可能正在寻找的答案是您没有在查询中选择
UserID
。但你还是应该看看我下面写的。您应该使用
if ($login_check->rowCount() == 1)
而不是count()
或者,最好使用
fetch( )
用户名的密码并在 php 中检查它是否正确,因为这样您就可以检测并存储每个用户的错误密码尝试,并且如果尝试次数过多,可能会暂时拒绝访问。Edit: The answer you are probably looking for is that you aren't selecting
UserID
in your query. But you should still look at what I wrote below.You'd should use
if ($login_check->rowCount() == 1)
instead ofcount()
Alternatively, it would be better to
fetch()
the password for the username and check it in php to see if it's correct because then you can detect and store bad password attempts for each user and possibly deny access for a while if there have been too many.