Greetings all. Im somewhat confused regarding the use of connection handling features of PHP.
Im building a chat application, the output frame is basically a script that runs forever. The point is that i need to know if the user disconnected / reloaded / pressed the stop button of his browser / closed his browser, in order to clean the DB.
Here is some bits and pieces of code :
<?
set_time_limit(0);
// ignore_user_abort(true);
require_once 'lib/Log.php';
$mask = Log::UPTO(PEAR_LOG_DEBUG);
$cnf = array('mode' => 0600, 'timeFormat' => '%x %X');
$logger = &Log::singleton('file', 'logs/system.log', 'agora', $cnf);
$logger->setMask($mask);
$logger->log("[output.php] Connection established for " . $_GET['cu'], PEAR_LOG_DEBUG);
$logger->flush();
while(!connection_aborted())
{
// chat code here
}
register_shutdown_function('close');
function close()
{
global $logger;
if(connection_aborted())
{
$logger->log("[output.php] Connection closed for " . $_GET['cu'], PEAR_LOG_INFO);
}
}
?>
Weird fact #1 : The log entry "Connection established" is only shown in the logs after i do an explicit reload of the frame, if i press the stop button of my browser, or if i close the browser.
Weird fact #2 : If uncomment the ignore_user_abort(true) function, then my script runs forever and ever, and is soon taking all the CPU ressources on the machine.
Im running PHP's CGI btw, dont know if this is relevant or not.
Any pointers appreciated 🙂
Thanks in advance.