Possible Duplicate:
Find Number of Open Sessions
Access active sessions in PHP
When a user arrives to a webpage, a session variable is created for the user and I wonder if it's possible to count the numbers of active sessions?
Possible Duplicate:
Find Number of Open Sessions
Access active sessions in PHP
When a user arrives to a webpage, a session variable is created for the user and I wonder if it's possible to count the numbers of active sessions?
When a user arrives to a webpage, a session variable is created for the user and I wonder if it's possible to count the numbers of active sessions?
Everything you store something can be counted. However, by default, values in a sessions are per user, PHP does not track all sessions all together or something.
So it's up to you to create that context. For example, you can write your own session handler.
That got much easier with PHP 5.4 because you can now extend the standard session handler and just add the functionality you need. Please see the manual page: The SessionHandler class
So how could this work? Each time the session is there, you write into a datastore that is shared that there is an active session. If the session get's destroyed, e.g. by garbage collection or because you compare against the time and enough time has passed since the last activity, you then can count that via the shared datastore.
Sessions are stored in temp files in the session.save_path (in ini file) folder. Each file begin with "sess_". I think you could count the number of these files to know how many sessions are active.
You could probably get the number of sessions with this:
$sessionCount = count(glob(session_save_path()."/*"));
However, you will need to make sure that session garbage collection is configured correctly so you don't get dead session files in there.