Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 881
Langkiller Avatar asked Jul 24 '26 14:07

Langkiller


2 Answers

One more time, can you do a quick try:

<?php
    session_start();

    $helper = array_keys($_SESSION);
    foreach ($helper as $key){
        unset($_SESSION[$key]);
    }
?>
like image 95
Hoan Huynh Avatar answered Jul 26 '26 03:07

Hoan Huynh


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

like image 24
Mosin Avatar answered Jul 26 '26 03:07

Mosin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!