加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-25 08:40:20 所属栏目:PHP 来源:互联网
导读:我已经挣扎了好几个小时,但我无法让它发挥作用.当我重定向到另一个 PHP页面时,我的所有会话变量都为null.我在xampp服务器上. session.php文件 ?php session_start(); if(isset($_POST[submitted]))

我已经挣扎了好几个小时,但我无法让它发挥作用.当我重定向到另一个 PHP页面时,我的所有会话变量都为null.我在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:xampphtdocssecure loginsession2.php on line 28

我尝试过其他一些事情,例如将会话变量从xampp / tmp保存到另一个目录,但这并没有解决问题.我有一个程序,当我进行重定向时,我需要让用户登录,但这已经阻止了我超过一天.

更新:

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

见 – > Killing off Global Session Variable as a logout button

好像你有问题,因为你有一个名称安全登录的空间
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();
     }
?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读