Reputation: 3457
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
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
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