duanbei1903 2015-03-03 07:08
浏览 33
已采纳

三个php会话之一没有设置

I have working session variables of username and password for login in, but when I try to add another session to show an attempt at login, it doesn't register. What I have working so far is this:

        // other php above where I do a query 
        session_start(); 
        if ($numrows == 0){ 
                //echo "no users matching that query
";        
                unset($_SESSION["username"]); 
                unset($_SESSION["password"]);       
                header ("location: welcomepage.php");    
        }   
        else {
                //echo "congradulations, you have loged in
"; 
                $_SESSION["username"] = $usernameSanitized; 
                $_SESSION["password"] = $passwordHashed;
                header ("location: welcomepage.php");    
        }   
?>

and in my welcomepage.php I have.

<?php
        session_start(); 
        if (isset($_SESSION["username"])&&isset($_SESSION["password"])){
                echo "hello".$_SESSION["username"]."
"; 
        }    
        else{
                echo "you have not yet logged in"; 
        }   
?>

This is fine, but for some reason, where I run into problems is when I try to add another third session to try to mark where user tried to login, called $_SESSION['attempt'].

In the first part of my code for the login, I add:

        // 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
";        
                unset($_SESSION["username"]); 
                unset($_SESSION["password"]);       
                header ("location: welcomepage.php");    
        }   
        else {
                //echo "congradulations, you have loged in
"; 
                $_SESSION["username"] = $usernameSanitized; 
                $_SESSION["password"] = $passwordHashed;
                header ("location: welcomepage.php");    
        }   
?>

And then I add another conditional:

<?php
        session_start(); 
        if (isset($_SESSION["username"])&&isset($_SESSION["password"])){
                echo "hello".$_SESSION["username"]."
"; 
        }   
        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"; 
        }   
?>

But when I enter with a wrong username or password, I'm always directed to "you have not yet logged in". What am I missing?

Thanks.

  • 写回答

1条回答 默认 最新

  • dounei9043 2015-03-03 07:23
    关注

    SOLUTION:

    I found a much better solution to my problem. I don't know why my conditional wasn't registering the session variable as isset, but my purpose was to send a variable depending on the login status (fail or success). I did this by doing the following:

            // other php code above
            $_SESSION["usermessage"] = "";  // set this depending on outcome         
            if ($numrows == 0){
                    //echo "no users matching that query
    ";        
                    $_SESSION["usermessage"] = "sorry, login failed";     
                    unset($_SESSION["username"]); 
                    unset($_SESSION["password"]);   
                    header ("location: frontpage.php");     
            }   
            else {
                    //echo "congradulations, you have loged in
    "; 
                    $_SESSION["usermessage"] = "you have logged in!"; 
                    $_SESSION["username"] = $usernameSanitized; 
                    $_SESSION["password"] = $passwordHashed;
                    header ("location: frontpage.php");     
            }   
    ?>
    

    and as well on my welcomepage.php, I could have tested a conditional based on the value of my $_SESSION["usermessage"] as well if I needed to, but instead I just displayed it with different values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab