会话变量有什么问题

What is wrong with the 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 完成处理 post 操作后重定向回 file1 时,它没有打印出会话变量 $_SESSION['testing']['testing'].. .这是怎么回事???

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