EDITED
I’ve got an ajax call (using $.ajax()) which calls the following php script.
for ($i=0;$i<40;$i++) {
echo " ";
flush();
if (connection_aborted()) {
log_message('error','CONNECTION IS ABORTED!!!!!');
exit;
}
else {
log_message('error','connection not aborted :(');
}
sleep(1);
}
This goes for 40 Seconds.
If I close the browser window which triggered the call, connection_aborted() still returns false, even if I sent explicitly a string and flushed the buffer!
Does anyone have an answer here please?
You will need to add “ignore_user_abort(true);” on top of the PHP script, and to call “ob_flush()” after echoing something from script (For why see PHP flush() man page). Working example (proof of concept):
P.S. connection_status() returns 0 if connection is still active, and in case of closed one returns 1.
EDIT:
My bad. Call both flush() and ob_flush() (please read flush() man page, link above, and answers from this topic), or otherwise might not work, depending on server/php configuration.
The following code was tested on WAMP with PHP 5.3.8 (works without calling flush()), and now on Ubuntu with PHP 5.3.10. where flush() call before ob_flush() is necessary.
Full code for testing:
index.html:
script.php:
After you call index.html page, and close tab or whole browser you should see in log.txt file next info: