Kaung
Kaung

Reputation:

$_SESSION List in PHP

I would like to get session list from the localhost.So, I wrote

session_start();
print_r($_SESSION);

currently access person is two.one is from another PC and the last one is hosted pc.But the on shown only hosted pc session.Actually I would like to get all session user list.

Upvotes: 6

Views: 18820

Answers (5)

This is possible as long as you know the identifier of the session which you want to handle. Here is a concrete example:

<?php
// Getting your own session id
$currentSessionId = session_id();

// Closing your own session
session_write_close();

// Switch to the session to handle
// Here, it is assumed that you pulled the session ID from a storage
session_id($sessionId);
session_start();

// Do whatever you want with the session ...

// Close the session once you finished to work with it
session_write_close();

// Restoring your own session session
session_id($currentSessionId);
session_start();

// At this point, you're back in your own session

Upvotes: 2

jondinham
jondinham

Reputation: 8499

getting the "ses_*" files from session_save_path is one option, but if these session files are at multi-level: http://www.php.net/manual/en/session.configuration.php#ini.session.save-path problems may arise.

the best is after every session_start(), update a mysql table which has the first column is session_id(), and the second is serialized data of $_SESSION

Upvotes: 0

Alix Axel
Alix Axel

Reputation: 154653

This can easily be done with the following code:

<?php

$sessions = array();

$path = realpath(session_save_path());
$files = array_diff(scandir($path), array('.', '..'));

foreach ($files as $file)
{
    $sessions[$file] = unserialize(file_get_contents($path . '/' . $file));
}

echo '<pre>';
print_r($sessions);
echo '</pre>';

?>

Make sure you understand the risks in doing (or allowing) this.

Upvotes: 12

deceze
deceze

Reputation: 522522

Sessions are isolated from each other. One user's session doesn't have access to another user's session. The variable $_SESSION references only the current user's session.

I don't think there's a way to access a different user's session other than finding out where the session files are physically stored on disk and parsing them by hand. NOTE THAT THIS IS NOT USUALLY SOMETHING YOU WANT TO DO.

Upvotes: 4

Darryl Hein
Darryl Hein

Reputation: 145107

I'm not sure what you are asking, but I think you are looking to get another session from another user. If this is the case, then I wouldn't recommend it and it's not really possible. Sessions are individual to each browser session (unless specified otherwise through cookies).

If you are asking to retrieve your sessions from your own user from another computer, then again, the default session behaviour is not really made to do this. You would need to implement your own session manager that probably uses a database and keeps track of which session is for which user (user id probably) so it can load it on another computer. I wouldn't recommend this either because you're getting into another whole ball of wax, per say.

If you are trying to keep track of data across user logins and computers, I would recommend using a user settings table. Here you can track the settings that the user has and load them each session or even each page load without having to modify the session handler.

If from your title you are wanting to get a list of currently active sessions, then simply record the last hit time of each user in a table and display that data, having a set time when you consider the session inactive.

Upvotes: 1

Related Questions