Langkiller
Langkiller

Reputation: 3457

Destroy all session variables with PHP

I am trying to get rid of a bunch of session variables in PHP. I mean completely get rid of them.

I have tried some different approaches. For example:

$_SESSION = array();
session_destroy();
header('location: '.MAINPATH);

I have also tried using various compinations of session_unset, unset, setcookie etc. with the above commands. I have of course tested if the session variables remains by doing:

echo $_SESSION['member_id'];

All of my session variables still remains for som reason. Can anyone figure out what the problem might be?

Any help is greatly appreciated.

FWI: I am using PHP 5.5

UPDATE: I tried changing my code to the following:

echo $_SESSION['member_id'];
session_unset();
session_destroy();
echo $_SESSION['member_id'];

which resulted in this output:

1000004 Notice: Undefined index: member_id in...

This should mean that the session variable is deletede right? The weird thing is, that when I am going back to my front page, the session variable is available again.

Upvotes: 3

Views: 18696

Answers (3)

Mosin
Mosin

Reputation: 608

you can use this code :-

session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);

use within php tag

Upvotes: 1

Hoan Huynh
Hoan Huynh

Reputation: 362

One more time, can you do a quick try:

<?php
    session_start();

    $helper = array_keys($_SESSION);
    foreach ($helper as $key){
        unset($_SESSION[$key]);
    }
?>

Upvotes: 5

Hoan Huynh
Hoan Huynh

Reputation: 362

You can use session_unset() to clear all session variables.

Upvotes: -3

Related Questions