PHP聊天会话

I have already build a chat script for my client's site. Once the user gets logged in I set a session and also update a table in a database by setting chat status as 1. Once the user gets logged out, I set the chat status to 0, furthermore if the user closes the tab of the browser the chat status is set to be 0 and no more chatting takes place.

The main issue here is how to detect that the user is not available for chat if the computer of the user gets shut down or the power gets cut off. Or even if the user opens a new tab and instead of closing the tab he closes the browser?

I am stuck. Thank you in advance

More or less what jhonraymos had said. If this is a PHP-based chat, then you can't really tell something like when the connection was forcefully closed easily. Simply have some sort of method to have a timeout for the chat, I would say.

IE: If there are no requests from the client for a few minutes, or maybe as long as a few hours, then it's safe to assume they are no longer connected, and then you can set the chat status as 0.

Update:

Apologies, I hadn't seen your new comment. Looking at another page...

session_start();
// set timeout period in seconds
$inactive = 600;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
    $session_life = time() - $_SESSION['timeout'];
    if($session_life > $inactive)
    { session_destroy(); header("Location: logoutpage.php"); }
}
$_SESSION['timeout'] = time();

Apologies again, still getting used to stackoverflow.

Just make a event in MYSQL with phpmyadmin to make status = 0 by every 1 min ( update login_table set status = '0').

then make a SetInterval to update status = 1 every 30 sec.

make another SetInterval to check status = 1 and retrieve all to DIV (this will display online users.