How to know that the user logged out



Hi everybody!

I wonder how do we know that the user logged out, if he/she didn't hit the log out button. Just close the browser.

May be I have already posted this question on this site, but I'm still not sure how to deal with it.

Hope anyone can solve my problem.:help:
Thanks in advance.

Makara Kao.
By default a session ends when the user closes the browser.
You can force this option by using:


To check if the user session exists (is logged in/out), make something like:


if(isset($_SESSION['user'])) {
     print 'User is logged in!';
} else {
     print 'User has gone!';

I suppose you're using sessions right ? :)
Thanks for your response, gesf.

Yes, I used the session to keep track of users.

But in the case that I want to count the number of registered users online:
-When he/she logs in I increase the number by 1.
-When he/she logs out I decrease the number by 1.

So is there any way to know that he/she logs out and decrease the number by 1 immediately when he closes the browser without clicking log out button.

Hope you will offer me some ideas or solution.
And someone also.


Makara Kao
this is the most typical of situations,
what you need to do is...
make an entry of the session in the db, and on every single page, update that sessions last page access timestamp, if you find sessions whose last-accessed-timestamp is more than 5-10 mins, consider them dead.
check, this forum itself, it uses same funda.