user2352103
user2352103

Reputation:

Logout with session_destroy() doesn't work

I'm working on a login/logout script. I can't understand why the logout doesn't work: I have tried every possible way, but it just doesn't destroy the session.

if($_GET['action'] == 'logout') {
    $_SESSION = array(); 
    session_destroy();

    header("location: /index.php");
    exit();
}

I don't have to add session_start() beacause the code is in the homepage, and the session is yet started.

Thank you in advance.

EDIT

It actually works when I click on the logout button, in fact i get on the home page and I'm no more logged in. But if I go on another website, and then I go back on mine, I'm logged.

AND, even more strange: With Internet Explorer, it works. How is this possible? I mean, I was used to differences between browser with CSS, not with PHP :)

Upvotes: 0

Views: 4817

Answers (3)

jyotisankar
jyotisankar

Reputation: 144

Please add session_start(); at the top of the page and then try with:

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

Upvotes: 1

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

On the top of the page

session_start();

Other wise the following will not work

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

Upvotes: 2

jjurm
jjurm

Reputation: 72

Try removing all cookies or only those which are used for login authentication.

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

Upvotes: 0

Related Questions