douningchang3610 2015-09-29 02:51
浏览 19
已采纳

会话变量没有进入另一个内部文件PHP

I have one login form when user give username and password it leads to login.php file

session_start();
if ( isset( $_POST['username'], $_POST['password'] ) ) {  
    $user   = $_POST['username'] ;
    $pass   = $_POST['password'] ;
    $query  = " MY QUERY "; 
    $result = mysql_query($query) or die('SQL ERROR:'.mysql_error()); 
    $row    = mysql_fetch_assoc($result);   
    if ($row) {         
        echo "query successfull wrote to DB";       
        unset($_SESSION);
        $userName = $row['firstname'].' '.$row['lastname'];
        $_SESSION['userNameSession'] = $userName;
        $_SESSION['loginStatus']     = '1';
        header('location:admin/admin.php');
    }else{
        echo "unscccessful login";
        header('location:index.php');
    }
}

When I Try to print the session by print_r($_SESSION) from this file.. it shows the session and its variable with values

Array ( [userNameSession] => full name [loginStatus] => 1 )

In my admin/admin.php (opens when successful login) wrote

    session_start();
    print_r($_SESSION);exit;

if try to print the session by print_r($_SESSION) it shows empty array as Array()

Please help.

  • 写回答

2条回答 默认 最新

  • duano3557 2015-09-29 03:18
    关注

    Why do you make an unset($_SESSION)? This may cause the session variable is deleted but the session still exists.

    If you want to clean $_SESSION['LoginStatus'] and $_SESSION['userNameSession'], better clean one by one (although this is not necessary because you'll rewrite its value later):

    unset($_SESSION['LoginStatus']);
    unset($_SESSION['userNameSession']);
    

    The code must be like this:

    session_start();
    if ( !empty($_POST['username']) && !empty($_POST['password']) ) {  
        $user   = $_POST['username'] ;
        $pass   = $_POST['password'] ;
        $query  = " YOUR QUERY "; 
        $result = mysql_query($query) or die('SQL ERROR:'.mysql_error());   
        if (mysql_num_rows($result) > 0) {        
            //DELETE prints BEFORE header()!! -> echo "query successfull wrote to DB"; 
            $row = mysql_fetch_assoc($result);
    
            unset($_SESSION['userNameSession']);
            unset($_SESSION['loginStatus']);
    
            $userName = $row['firstname'].' '.$row['lastname'];
    
            $_SESSION['userNameSession'] = $userName;
            $_SESSION['loginStatus']     = '1';
    
            header('location:admin/admin.php');
        }else{
            //DELETE prints BEFORE header()!! -> echo "unscccessful login";
            header('location:index.php');
        }
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 设计一个光控计数器,全部用ttl芯片
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部