会话没有被破坏

问题描述 投票:0回答:5

我有这个文件

secure.php

session_start();
if(empty($_SESSION['u_name'])) {
    header("Location:emprego.php");
}

if(isset($_GET['logout'])) {
    session_destroy();
    header("Location:emprego.php");
}

$name = $_SESSION['u_name'];

?>

<li><?php echo "<a href='emprego.php?logout' id='D'>Logout</a>";?></li>

基本上,如果我注销,我将被重定向到emprego.php。但如果我点击后页按钮(浏览器中的箭头),我可以查看同一页面(secure.php)。

我的问题是,为什么?

谢谢

php session logout
5个回答
2
投票

http://nl2.php.net/manual/en/function.session-destroy.php

看看这里的例子1。它明确指出你必须清除$ _SESSION。

if(isset($_GET['logout'])) {
    unset($_SESSION['u_name']); //makes it non-existent (it does unset) that variable
    session_destroy();
    header("Location:emprego.php");
}

0
投票

您的浏览器会在缓存中保留页面的副本。单击后退按钮时,您将看到本地缓存副本,而不是服务器中的当前页面。如果您的安全性设置正确,您将无法从该缓存页面执行任何有意义的操作。

出于这个原因,安全网站(例如银行网站)会在您注销后告诉您注销并清除缓存(或关闭浏览器)。


0
投票

如果您正在使用会话cookie,请尝试明确地使会话cookie过期,如下所示:

if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

此外,返回浏览器只会加载页面的缓存副本。如果您尝试与缓存页面进行交互以从服务器获取新页面,则您应该无法继续。


0
投票

我最近发现了header_remove(); http://php.net/manual/en/function.header-remove.php

    Caution: This function will remove all headers set by PHP, including cookies, session and the X-Powered-By headers.

不确定这是否是适当的方法,但它对于注销功能非常有效。


0
投票

所有其他解决方案似乎都不适合我。但是,这种解决方法可以解决问题。基本上,下面的代码一直调用注销,直到注销最终成功:

if (isset($_GET["logout"])){
    if (isset($_SESSION["username"])) {
        unset($_SESSION["username"]);
        session_destroy();
        header("Location:/?logout=true");
        exit;
    }
    header("Location:/");
    exit;
}
© www.soinside.com 2019 - 2024. All rights reserved.