Read all session data from session storage file in php.

@mit $ingh. July 13, 2016 Comments

If you want to decode session data, use session_decode (see the manual). unserialize only decodes single variables, not session data.

You can do something like:

//get session storage path
$dir = ini_get("session.save_path");

//get all session files
$sessFiles = preg_grep("/^sess_/", scandir($dir));

//get all session data by looping the files.
foreach ($sessFiles as $key => $value) {
    
    $contents = file_get_contents($dir . $value);
    @session_start();
    session_decode($contents);
    echo '<hr><pre>';
    print_r($_SESSION);
}

 

If you want to keep session data in another variable you can do like below.

// if session is not started
session_start();

// store our current session
$my_sess = $_SESSION;

// decode $contents (the encoded session data, either from a file or database). Remember, decoded data is put directly into $_SESSION
session_decode($contents);
$data = $_SESSION;

print_r($data);

// restore our own session
$_SESSION = $my_sess;

 

  • PHP
  • session