drn5375 2015-07-14 04: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-14 04: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 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题