PHP Members Logged In Count

Given that I have a working login function which verifies against a database, I would like a simple counter on my login page that shows:

Members Logged In : 9

Every time the login is successful, a global variable should be incremented. When the user logs out or the user's session dies, this global variable should decrement also.
fcqmaxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
SPARC-DESIGNConnect With a Mentor Commented:
hi there,
could this be usefull: http://www.sim-php.info/users-online-script.php
0
 
marty_tConnect With a Mentor Commented:
Hi,

Best way, being as you're already programming with a DB, is to create a table within that DB for Login Sessions. This can simply have the Users ID in it with a timestamp that gets updated at each page view.

Every time it's updated, purge the data older than say 10 minutes, then count on the amount of rows you have. That will give you the amount of logged in users.

When a user logs out, it deletes them from the table.

Hope that helps.

Marty
0
 
SPARC-DESIGNCommented:
@marty_t
If you would have clicked the link I posted, you wouldn't type all the text ;-)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
marty_tCommented:
haha, too true
0
 
fcqmaxAuthor Commented:
I understand the logic now, but what happens if a user just quits the browser without clicking the logout link to cleanly log out?
0
 
SPARC-DESIGNCommented:
I understand the logic now, but what happens if a user just quits the browser without clicking the logout link to cleanly log out?
------
The session will die after a few minutes and the row will be deleted from the DB
0
 
fcqmaxAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.