Skip to content Skip to sidebar Skip to footer

How To Count Sessions To Display Total Number Of Logged In Users On The Site

I want display logged in online users on my site like Members Online: 102. I am not catching a perfect and simple way as how to do this. Whether there be a session counting method

Solution 1:

It's not possible to detect a closing browser. The best way to know how many users you have on the site is to do it all with sessions. When you use the sessions on file system it's pretty hard because you have to open all the files and read them. So....

The best way to do it is to save your sessions in the database. There you can do set a user id to an session on login and remove it on logout. Then you can count all sessions with an user is with a simple mysql count script.

Here a link with a nice tutorial how to do sessions on a database.

http://www.tonymarston.net/php-mysql/session-handler.html

You still have one more problem. When a user closes his browser you will not see that. This is because a client don't send a message. i'm gone now. That's why we have a session timeout. you can shorten the timeout to say 2min but then you have a problem that when someone is reading a page for more than 2min he will be kicked. You can compensate that with an ajax script every min to update the session but that will take a lot of server traffic/memory. That's why that is not recommended.

Advice: Set your sessions to the database with an timeout of 20min. When someone closes his browser you won't see that for max 20min.

Post a Comment for "How To Count Sessions To Display Total Number Of Logged In Users On The Site"