使用session_destroy()注销不起作用


Logout with session_destroy() doesn't work

我正在编写一个登录/注销脚本。我不明白为什么注销不工作:我已经尝试了所有可能的方法,但它就是不破坏会话。

if($_GET['action'] == 'logout') {
    $_SESSION = array(); 
    session_destroy();
    header("location: /index.php");
    exit();
}

我不需要添加session_start(),因为代码在主页中,会话还没有开始。

提前谢谢你。

编辑

当我点击退出按钮时,它实际上是有效的,事实上我进入了主页,我不再登录。但是,如果我访问另一个网站,然后再返回到我的网站,我就被登录了。

更奇怪的是:在ie浏览器中,它可以工作。这怎么可能呢?我的意思是,我习惯了不同浏览器之间的差异使用CSS,而不是PHP:)

在页面顶部

session_start();

否则

将不起作用
if($_GET['action'] == 'logout') {
  $_SESSION = array(); 
  session_destroy();
  header("location: /index.php");
  exit();
}

请添加session_start();在页面的顶部,然后尝试使用:

if($_REQUEST['action'] === 'logout') {
  $_SESSION = array(); 
  session_destroy();
  header("location:index.php");
  exit();
}

尝试删除所有cookie或仅删除用于登录身份验证的cookie。

if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

http://www.php.net/manual/en/function.setcookie.php 73484