Destroy all session variables with PHP

14,039

One more time, can you do a quick try:

<?php
    session_start();

    $helper = array_keys($_SESSION);
    foreach ($helper as $key){
        unset($_SESSION[$key]);
    }
?>
Share:
14,039
Langkiller
Author by

Langkiller

Updated on June 16, 2022

Comments

  • Langkiller
    Langkiller almost 2 years

    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.