there are two things to do
1. add user alongwith userID and session and timestamp to table when they come to your site
- to delete all thse deatils once they leave/logout etc.
when they logout explicitly, it is easy for you just delete it in the "successfully logged out or thank you "page
problem is when user doesnt explicitly log out
for this you have the time stamp field.
the function
get_cfg_var("session.gc_maxlifetime");// return seconds
will return you the maximum time a session is alive when there has been no activity from user
so you can keep check of this and in your main page where you count the number of users, run this check and whatever entries in db are inactive beyond this time(or any time selected by you) delete them, so your table is reasonably realtime.
makes sense?
shoot questions if any doubts..