Get online user list and remove unactive user from list


I am try to make function how get currently online user on my website…
I am saving user_id in table but i am facing one problem when i close my browser without logout my system still showing me login because my user_id is store in DB.

how can i know that the user is actually online means how can i get session is set or destroy.

or if you can best to get online user please tell me.

you can see get my website links from my signature and you can find login user below footer


You should be wiping out any session/cookie data during the logout process, so when they click logout, you should be deleting cookies and using unset($_SESSION) to wipe out the session variables.

It looks like he’s talking about when the browser is closed without the user logging out.

Unfortunately 12345qwer, there is no way to detect a closed browser in PHP. There are some JavaScript/AJAX methods but they’re far from fool proof. Your best bet is to add something like a last_activity datetime field to your “active users” table in your DB. Then, whenever a logged in user does anything on your site (views a page, makes a post, etc), update your DB’s last_activity field with the new timestamp.

Then you have two options.
If you have MySQL 5.1.6 or greater, you can set up an event in your DB to run say, every 15 minutes to check for users who have been inactive for however long and remove them from your active users table. Events in MySQL are the MySQL equivalent to linux cron jobs only they’re managed and run by the DB server specifically for DB tasks.

If your version/setup doesn’t allow for Events, then set up a cron job to run say, every 15 minutes and call a PHP script to do the same.

Well put. I would lean to toward the cron job in case you have any other tasks you may want to accomplish that may not be suited for MySQL.

Yeah, a cron may be better for this type of thing. After all, this type of thing would fall under “application logic” and probably shouldn’t be the job of the DB.

Agreed. Thinking on it further, one could log activity in a table and then present an active user list using criteria on last active, then running a cron job once a day to clean that table up. Might be a better idea than relying on that cron job to do its job to present the correct data, a cron job failure here would only result in wasted table space.

Thanks, I missed that part.

thanks for your help…now i can do this…
I don’t know about cron job can you tell ?

I am getting data using ajax my page area here my online will be show is refreshing after 10 seconds.