drn5375 2015-07-13 20:08
浏览 74
已采纳

标题重定向后,PHP会话索引是否未定义?

I have struggled with this for hours but I can't get it to work. When I do a redirection to another PHP page, all my session variables are null. I am on xampp server.

session.php

<?php
     session_start();
     if(isset($_POST['submitted']))                                                                                         
     {   
        $_SESSION['first_name'] = "MAX";
        var_dump($_SESSION);
        header("Location: http://localhost:8080/secure login/session2.php");     
        die();
     }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-    1" /> 
    <title>You Logged In</title> 
</head> 
<body> 
    <form action="session.php" method="post">
        <div align="center"><input type="submit" name="submit" value="Login" /></div>
    <input type="hidden" name="submitted" value="TRUE" />
    </form>
</body>
</html>

session2.php

<?php
    session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
    <title>You Logged In</title> 
</head> 
<body> 
    <div id="main"> 
        <?php 
            echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
            echo 'You are welcome to session2.php <br></br>'; 
            if (isset($_SESSION['first_name'])) 
            { 
                echo $_SESSION['first_name'] . "<br></br>";
            }
            else
            {
                echo "Your session doesn't exist. I hate php <br></br>";
                echo $_SESSION['first_name'];
            }
        ?>
    </div>
 </body>
 </html>

The session doesn't save, and the output is;

Array
(
)
You are welcome to session2.php
Your session doesn't exist. I hate php
Notice: Undefined index: first_name in C:\xampp\htdocs\secure login\session2.php on line 28

I have tried other things like changing where session variables are saved from xampp/tmp to another directory, but this didn't solve the problem. I have a program that I need to keep a user logged in when I do a redirection but this has blocked me for more than a day.

UPDATE:

The space between the directories wasn't the problem, it temporarily solved the problem but that was because there wasn't cache for the new directory yet. Any way, for a few more days, I debugged and realized that I was running two programs on my localhost. Both were using sessions, and so if one terminates the session, it also terminates the session for the other since localhost is like a domain name and there exists only one session. Particularly, the logout.php of my other program was not destroying the session but was rather jumbling it up were by you have to remove browser cache do unjumble it. I was emptying session array, destroying the session, and destroying the cookie, this was the problem and so I couldn't login again. All I had to do was just destroy the session only;

See -> Killing off Global Session Variable as a logout button

展开全部

  • 写回答

4条回答 默认 最新

  • doushun1904 2015-07-13 20:54
    关注

    Seems like you are having problem because you have a space in name secure login

    localhost:8080/secure%20login/session.php 
    

    So please try to change the name with underscore secure_login and also change your code

    <?php
         session_start();
         if(isset($_POST['submitted']))                                                                                         
         {   
            $_SESSION['first_name'] = "MAX";
            var_dump($_SESSION);
            header("Location: http://localhost:8080/secure_login/session2.php");     
            die();
         }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
手机看
程序员都在用的中文IT技术交流社区

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

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

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

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

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

客服 返回
顶部