标头重定向后,PHP会话索引未定义? [英] PHP session index undefined after header redirection?

查看:173
本文介绍了标头重定向后,PHP会话索引未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了好几个小时,但我无法使它正常工作.当我重定向到另一个PHP页面时,我所有的会话变量均为null.我在xampp服务器上.

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

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

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

我尝试了其他操作,例如将会话变量从xampp/tmp保存到另一个目录的位置更改,但这并不能解决问题.我有一个程序,当我进行重定向时,需要使用户保持登录状态,但这已使我封锁了一天多的时间.

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.

更新:

目录之间的空间不是问题,它暂时解决了问题,但这是因为还没有为新目录缓存.无论如何,又过了几天,我调试并意识到我正在本地主机上运行两个程序.两者都在使用会话,因此,如果一个会话终止,则另一个会话也会终止,因为localhost就像一个域名,并且仅存在一个会话.特别是,我其他程序的logout.php并没有破坏会话,而是将其弄乱了,因为您必须删除浏览器缓存才能使会话混乱.我清空了会话数组,破坏了会话,破坏了cookie,这就是问题所在,所以我无法再次登录.我要做的就是只破坏会话;

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;

请参阅-> >注销全局会话变量作为注销按钮

推荐答案

似乎有问题,因为名称中有空格secure login

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

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

因此,请尝试使用下划线secure_login更改名称,并更改您的代码

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();
     }
?>

这篇关于标头重定向后,PHP会话索引未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆