【问题标题】:What is wrong with the session variables会话变量有什么问题
【发布时间】:2015-05-16 03:29:28
【问题描述】:

我不知道我的代码中的会话变量有什么问题...这是一个 sn-p:

文件 1:

display_test();

    function display_test(){
        if(isset($_SESSION['testing']['testing'])) echo $_SESSION['testing']['testing'];

        echo "<br><br><br><form id=\"current_juices_form\" method=\"post\" action=\"file2.php\">
                <input type=\"submit\" />
             </form>";
    }

文件2(上面表格也提交的文件):

testing();

function testing(){
    unset($_SESSION);
    $_SESSION['testing']['testing'] = "<br>testing<br>";
    header("Location:  file1.php");
}

由于某种原因,在 file2 完成处理后操作后重定向回 file1 时,它没有打印出会话变量 $_SESSION['testing']['testing']...这是怎么回事???

【问题讨论】:

  • 你有 session_start();在两个文件中?
  • 是的,我仔细检查了...
  • 你在调用/返回函数吗?
  • 将错误报告添加到文件顶部,紧跟在打开 PHP 标记之后,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,看看它是否会产生任何结果。

标签: php forms redirect post session-variables


【解决方案1】:

您需要先初始化 $_SESSION['testing']。

function testing(){
    if (!isset($_SESSION['testing'])) {
        $_SESSION['testing'] = Array();
    }

    $_SESSION['testing']['testing'] = "<br>testing<br>";
    header("Location:  file1.php");
}

或者你也可以这样做:

function testing(){        
    $_SESSION['testing'] = Array('testing' => "<br>testing<br>");
    header("Location:  file1.php");
}

【讨论】:

  • 我说的是什么,但在我的代码的其他部分,我从未初始化二维会话变量,它们总是有效... id 发布其他代码,但它在 nda 下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2011-12-10
  • 2014-10-10
相关资源
最近更新 更多