Click to See Complete Forum and Search --> : PHP sessions


Maximus9999
09-25-2007, 12:55 PM
Does anybody know how I can get all sessions which are logged in? I'm trying to output a list of users which are currently available.

Znupi
09-25-2007, 01:35 PM
Here's how I'd do it:

$sessionDir = ini_get("session.save_path");
$files = `ls {$sessionDir}`;
$numberOfSessions = count(explode(" ", $files)); // Explode by 2 spaces, not one!

echo "Users online: {$numberOfSessions}";
?>

Of course, considering your server runs on linux. Hope this helps :)

Maximus9999
09-25-2007, 01:40 PM
Will this keep track of only people who have logged on?

Or will it update when people have logged off the session also?

Znupi
09-25-2007, 03:01 PM
It depends on how you coded your site. It just counts the number of sessions at one point.

If you call session_start() whenever a person accesses your site, then it will count all people online.
If you call session_start() only when someone successfully logs in, then it will count all logged in users.