tags:

views:

64

answers:

3

Without having to call each session by name, is there a way to display the contents of all sessions currently set?

A: 

print_r($_SESSION);

SpawnCxy
the question is asking for all sessions, not just the current session
mwhite
@Michael White,this is what OP need,so I think you should remove the downvote.
SpawnCxy
@SpawnCxy My answer provided this and was accepted, so I guess it was what the OP wanted. I will upvote you to offset the -1.
alex
A: 
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';

Or you can use print_r if you don't care about types. If you use print_r, you can make the second argument TRUE so it will return instead of echo, useful for...

echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
alex
The OP is asking about all the sessions.
kiamlaluno
@kiamlaluno You mean the sessions for every user?
alex
Well, he accepted it, so must have meant this I think...
alex
The title says "How to print all sessions currently set in PHP?"; if all the OP wanted was to print the current session, then the question is wrongly written.
kiamlaluno
@kiamlaluno I think what he wanted can be derived from the question. The title seems to say he wants all sessions set for each session, however, the question body says he wants them without having to call each session by name, which I assumed would be the key in `$_SESSION`. Though he could have been referring to the hash that is assigned in the cookie, I thought the former seemed more likely.
alex
+3  A: 

Not a simple way, no.

Let's say that by "active" you mean "hasn't passed the maximum lifetime" and hasn't been explicitly destroyed and that you're using the default session handler.

  • First, the maximum lifetime is defined as a php.ini config and is defined in terms of the last activity on the session. So the "expiry" mechanism would have to read the content of the sessions to determine the application-defined expiry.
  • Second, you'd have to manually read the sessions directory and read the files, whose format I don't even know they're in.

If you really need this, you must implement some sort of custom session handler. See session_set_save_handler.

Take also in consideration that you'll have no feedback if the user just closes the browser or moves away from your site without explciitly logging out. Depending on much inactivity you consider the threshold to deem a session "inactive", the number of false positives you'll get may be very high.

Artefacto
The files appear to be in this format `last_active|i:1280043940;`, so with a bit of splitting, I think we could produce a list (though not accurately *active* sessions)
alex