After the user successfully logs in, I store login = true in database. But how do I check if the user logged out by closing the browser without clicking the logout button? A
This is an extension to what 'ceejayoz' said before.
Let the users periodically ping the service and tell that they are still logged in. Store last ping time in the session. If the last ping session is greater than a said time, then ping again to tell that the use is still alive.
Store the time when you received the ping in the database. If the last ping received is > the keeplive threshold then consider the user has left the site.
The following is some untested sample code for you start with. You can use the "PING__FREQUENCY" to control how often the frequency in which the user activity will update the last_activity column.
define(PING_FREQUENCY,300); //5 mins
if (($_SESSION['lastPingTime'] + PING_FREQUENCE) > time()) {
stillLoggedIn(); //execute a function to tell that the user is still logged in
}
function stillLoggedIn() {
//Do SQL update to mark the last activity time for the user
}