三个php会话之一未设置

问题描述:

我有工作会话变量usernamepassword登录,但当我尝试添加另一个会话在登录时显示attempt,它不会注册。我到目前为止工作的是这样的:三个php会话之一未设置

 // 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"。我错过了什么?

谢谢。

+0

2015-03-03 07:20:44

SOLUTION:

我找到一个更好的解决我的问题。我不知道为什么我的条件没有注册会话变量为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"]的价值,如果我需要测试条件为好,而是我只是用不同的显示它值。