3

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?

Community
  • 1
  • 1
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159
  • Active Sessions all together by all users on the website? – hakre Jun 29 '12 at 19:59
  • possible duplicates: [Find Number of Open Sessions](http://stackoverflow.com/questions/679657/find-number-of-open-sessions), [Access active sessions in PHP](http://stackoverflow.com/questions/3426844/access-active-sessions-in-php), and [Looping Through All a Server's Sessions in PHP](http://stackoverflow.com/questions/675913/looping-through-all-a-servers-sessions-in-php) – mellamokb Jun 29 '12 at 19:59
  • Using the default session handler? `sess_*` files under `session.save_path`? – Leigh Jun 29 '12 at 19:59
  • Another related, possible duplicate: [Count active sessions](http://stackoverflow.com/q/4071764/367456) – hakre Jun 29 '12 at 20:00
  • AFAIK PHP doesn't keep an index of sessions, active or otherwise. What storage mechanism are you using? – Mike B Jun 29 '12 at 20:00
  • @3D-kreativ: If this is really a "must have" feature for your app, investigate custom session handling, it will make it far easier to do things like identifying which users have more than 1 session etc. (I think it was deviantart that tells me that I have sessions open from specific browser/OS/IP or something) – Leigh Jun 29 '12 at 20:04
  • I'd say as well, custom session handler will help you there. Running stats while saving sessions. Also put a timestamp into the session data and save it along with the bucket. Writing sessions handlers has become more easy btw. – hakre Jun 29 '12 at 20:05
  • OK, it's not a "must have" I was just interested to make some code to check how many active user a webpage have – 3D-kreativ Jun 29 '12 at 20:10

3 Answers3

3

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • If you just want experiment a bit, there is an example session file viewer in a library called Serialized, that might be interesting for you when you just want to play around apart from the session handler itself: https://github.com/ktomk/Serialized/blob/master/examples/03-session/example-viewer.php – hakre Jun 29 '12 at 20:21
  • This is an excellent use case for using database for sessions. Then you can literally just count the number of records that represent active sessions. – Concordus Applications Jun 29 '12 at 20:21
  • 1
    @ConcordusApplications: Yes, you can give it a timestamp column for example. However, for sessions, consider some storage that is faster, e.g. memory: [Not only SQL - memcache and MySQL 5.6](http://schlueters.de/blog/archives/152-Not-only-SQL-memcache-and-MySQL-5.6.html) – hakre Jun 29 '12 at 20:24
  • @hakre...I totally agree. DB is a slow way to handle sessions, but some type of central segregated storage becomes the only way to do this reliably. – Concordus Applications Jun 29 '12 at 20:26
2

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.

N. Hodin
  • 1,056
  • 6
  • 12
1

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592