PHP为什么$ _SESSION变量拒绝在浏览器上取消设置?

时间:2018-06-20 09:10:41

标签: php session unset

我对php比较陌生,但遇到一个错误,该错误将使我疯狂。

我正在创建一个网页,使您可以浏览服务器上的文件。

但是,由于某种原因,即使浏览器重新启动,$ _ SESSION变量也会保持自身设置状态。

拜托,我求你,告诉我为什么在我发疯之前会发生这种情况。

这是我的代码:

<html>
<?php
session_start();
if(!isset($_GET['location'])) {
    header('Location: ?location=G:');
    session_unset();
}
/* THIS IS WHERE THE BUG OCCURS. THIS VARIABLE SHOULD BE EMPTY ON BROWSER OPEN!!!! ?!?!?!?! I HAVE ADDED NOTHING TO SESSION YET*/
var_dump($_SESSION);
if(!isset($_SESSION['path'])) {
                $_SESSION['path'] = array();
                $_SESSION['path'][0] = $_GET['location'];
            }
echo '<br><br><br><br><br><br>';
//* If user presses back and isn't in home folder, return to previous folder... *//
if(isset($_GET['back']) && $_GET['back'] == true && sizeof($_SESSION['path']) > 0) {
                unset($_SESSION['path'][sizeof($_SESSION['path'])-1]);
                $_GET['location'] = $_SESSION['path'][sizeof($_SESSION['path'])-1];
                header ('Location: ?back=false');
            } else {
                //*However if user hasn't pressed back make sure that session is properly updated with location *//
                if($_SESSION['path'][sizeof($_SESSION['path'])-1] != $_GET['location']) {
                    array_push($_SESSION['path'], $_GET['location']);
                }
            }
            //*Now build the link from the session path array*//
            $full_link = '';
            for($i = 0; $i < sizeof($_SESSION['path']); $i++) {
                $full_link .= $_SESSION['path'][$i];
                $full_link .= '/';
            }
            //*Get all files from link path*//
            $filesbrowsed = glob($full_link . '*');
?>
    <head>
        <meta charset "utf8">
        <title>File Browser</title>
        <link href="filebrowserstyle.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <header>
            <ul class = "navigation">
                <li><a href = "">Home</a></li>
                <li><a href = "?back=true">Back</a></li>
                <li><a href = ""></a></li>
                <li><a href = ""></a></li>
            </ul>
        </header>
        <div class = 'current_files'>
            <?php
            //* Now display all files in current path *//
            for($i = 0; $i < sizeof($filesbrowsed); $i++) {
                $filename = substr($filesbrowsed[$i], strlen($full_link), strlen($filesbrowsed[$i]) - strlen($full_link));
                echo '<div><a href = "?location=' . $filename . '/' . '">' . $filename . '</a></div>';
            }
            ?>
        </div>
    </body>
</html>

提前谢谢大家!

2 个答案:

答案 0 :(得分:2)

在将用户重定向到另一个位置之前,您应该取消会话设置。

<html>
<?php
session_start();
if(!isset($_GET['location'])) {
    session_unset();
    session_destroy();
    header('Location: ?location=G:');
}
/* THIS IS WHERE THE BUG OCCURS. THIS VARIABLE SHOULD BE EMPTY ON BROWSER OPEN!!!! ?!?!?!?! I HAVE ADDED NOTHING TO SESSION YET*/
var_dump($_SESSION);

答案 1 :(得分:0)

要删除会话中的所有数据,请执行以下操作:

$_SESSION = [];