头重定向后,PHP会话索引未定义


PHP session index undefined after header redirection?

我已经挣扎了几个小时,但我不能让它工作。当我重定向到另一个PHP页面时,所有会话变量都为空。我在xampp服务器上。

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>

会话不保存,输出为;

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

我尝试过其他的事情,比如改变会话变量从xampp/tmp保存到另一个目录,但这并没有解决问题。我有一个程序,我需要保持一个用户登录时,我做一个重定向,但这已经阻止了我超过一天。

更新:

目录之间的空间不是问题,它暂时解决了问题,但那是因为还没有为新目录缓存。不管怎样,又过了几天,我调试了一下,发现我在本地主机上运行了两个程序。两者都使用会话,因此如果一个终止会话,它也会终止另一个的会话,因为localhost类似于域名,并且只存在一个会话。特别地,我的另一个程序的logout。php并没有破坏会话,而是把它弄乱了,因为你必须删除浏览器缓存来清理它。我清空会话数组,销毁会话,销毁cookie,这就是问题所在,所以我无法再次登录。我所要做的只是销毁会话;

参见->取消全局会话变量作为注销按钮

似乎您遇到了问题,因为您的名称secure login

中有一个空格
localhost:8080/secure%20login/session.php 

所以请尝试用下划线secure_login更改名称并更改代码

<?php
     session_start();
     if(isset($_POST['submitted']))                                                                                         
     {   
        $_SESSION['first_name'] = "MAX";
        var_dump($_SESSION);
        header("Location: http://localhost:8080/secure_login/session2.php");     
        die();
     }
?>

很可能是由于die()调用,我认为这导致它不写会话。

先试session_write_close();

修改

<?php
    session_start();
    if(isset($_POST['submitted']))
    {
        $user = $_SESSION['first_name'] = "MAX";
        if(isset($user))
        {
            header("Location: http://localhost:8080/secure login/session2.php");
        }
        else
        {
            header("Location: ");//index page
        }
    }
?>

In $user = $_SESSION['first_name'] = "MAX";

首先执行$_SESSION['first_name'] = "MAX";,然后将结果赋值给$user。所以在isset($user)中输入Check是否set

首先,检查session.phpsession2.php文件中<?php标签是否有空白。

var_dump($_SESSION);session.php中移除

并将文件夹名称更改为secure_login