Anwar Anwar - 4 months ago 14x
PHP Question

PHP chat session

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.


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

// 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.