三个 php 会话之一未设置
one of three php sessions not setting
我有 username
和 password
的工作会话变量用于登录,但是当我尝试添加另一个会话以在登录时显示 attempt
时,它没有注册.到目前为止我的工作是这样的:
// other php above where I do a query
session_start();
if ($numrows == 0){
//echo "no users matching that query\n";
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header ("location: welcomepage.php");
}
else {
//echo "congradulations, you have loged in\n";
$_SESSION["username"] = $usernameSanitized;
$_SESSION["password"] = $passwordHashed;
header ("location: welcomepage.php");
}
?>
在我的 welcomepage.php
我有。
<?php
session_start();
if (isset($_SESSION["username"])&&isset($_SESSION["password"])){
echo "hello".$_SESSION["username"]."\n";
}
else{
echo "you have not yet logged in";
}
?>
这很好,但由于某些原因,我 运行 遇到问题的地方是我尝试添加另一个第三个会话以尝试标记用户尝试登录的位置,称为 $_SESSION['attempt']
。
在登录代码的第一部分,我添加:
// other php above where I do a query
session_start();
$_SESSION["attempt"] = "attempted"; //<----added this line
if ($numrows == 0){
//echo "no users matching that query\n";
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header ("location: welcomepage.php");
}
else {
//echo "congradulations, you have loged in\n";
$_SESSION["username"] = $usernameSanitized;
$_SESSION["password"] = $passwordHashed;
header ("location: welcomepage.php");
}
?>
然后我添加另一个条件:
<?php
session_start();
if (isset($_SESSION["username"])&&isset($_SESSION["password"])){
echo "hello".$_SESSION["username"]."\n";
}
else if (isset($_SESSION["attempt"])){ // <--- added this condition
echo "login with username and password failed";
unset($_SESSION["attempt"]);
}
else{
echo "you have not yet logged in";
}
?>
但是当我输入错误的用户名或密码时,我总是被引导至 "you have not yet logged in"
。我错过了什么?
谢谢。
解决方案:
我找到了解决问题的更好方法。我不知道为什么我的条件没有将会话变量注册为 isset
,但我的目的是根据登录状态(失败或成功)发送一个变量。我通过执行以下操作做到了这一点:
// other php code above
$_SESSION["usermessage"] = ""; // set this depending on outcome
if ($numrows == 0){
//echo "no users matching that query\n";
$_SESSION["usermessage"] = "sorry, login failed";
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header ("location: frontpage.php");
}
else {
//echo "congradulations, you have loged in\n";
$_SESSION["usermessage"] = "you have logged in!";
$_SESSION["username"] = $usernameSanitized;
$_SESSION["password"] = $passwordHashed;
header ("location: frontpage.php");
}
?>
以及在我的 welcomepage.php
上,如果需要,我也可以根据我的 $_SESSION["usermessage"]
的值测试条件,但我只是用不同的值显示它。
我有 username
和 password
的工作会话变量用于登录,但是当我尝试添加另一个会话以在登录时显示 attempt
时,它没有注册.到目前为止我的工作是这样的:
// other php above where I do a query
session_start();
if ($numrows == 0){
//echo "no users matching that query\n";
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header ("location: welcomepage.php");
}
else {
//echo "congradulations, you have loged in\n";
$_SESSION["username"] = $usernameSanitized;
$_SESSION["password"] = $passwordHashed;
header ("location: welcomepage.php");
}
?>
在我的 welcomepage.php
我有。
<?php
session_start();
if (isset($_SESSION["username"])&&isset($_SESSION["password"])){
echo "hello".$_SESSION["username"]."\n";
}
else{
echo "you have not yet logged in";
}
?>
这很好,但由于某些原因,我 运行 遇到问题的地方是我尝试添加另一个第三个会话以尝试标记用户尝试登录的位置,称为 $_SESSION['attempt']
。
在登录代码的第一部分,我添加:
// other php above where I do a query
session_start();
$_SESSION["attempt"] = "attempted"; //<----added this line
if ($numrows == 0){
//echo "no users matching that query\n";
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header ("location: welcomepage.php");
}
else {
//echo "congradulations, you have loged in\n";
$_SESSION["username"] = $usernameSanitized;
$_SESSION["password"] = $passwordHashed;
header ("location: welcomepage.php");
}
?>
然后我添加另一个条件:
<?php
session_start();
if (isset($_SESSION["username"])&&isset($_SESSION["password"])){
echo "hello".$_SESSION["username"]."\n";
}
else if (isset($_SESSION["attempt"])){ // <--- added this condition
echo "login with username and password failed";
unset($_SESSION["attempt"]);
}
else{
echo "you have not yet logged in";
}
?>
但是当我输入错误的用户名或密码时,我总是被引导至 "you have not yet logged in"
。我错过了什么?
谢谢。
解决方案:
我找到了解决问题的更好方法。我不知道为什么我的条件没有将会话变量注册为 isset
,但我的目的是根据登录状态(失败或成功)发送一个变量。我通过执行以下操作做到了这一点:
// other php code above
$_SESSION["usermessage"] = ""; // set this depending on outcome
if ($numrows == 0){
//echo "no users matching that query\n";
$_SESSION["usermessage"] = "sorry, login failed";
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header ("location: frontpage.php");
}
else {
//echo "congradulations, you have loged in\n";
$_SESSION["usermessage"] = "you have logged in!";
$_SESSION["username"] = $usernameSanitized;
$_SESSION["password"] = $passwordHashed;
header ("location: frontpage.php");
}
?>
以及在我的 welcomepage.php
上,如果需要,我也可以根据我的 $_SESSION["usermessage"]
的值测试条件,但我只是用不同的值显示它。