会话变量有什么问题

时间:2015-05-16 03:29:29

标签: php forms redirect post session-variables

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

文件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 [&#39; testing&#39;] [&#39; testing&#39; ;] ......发生了什么?

1 个答案:

答案 0 :(得分:2)

您需要先初始化$ _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");
}