note 112746 deleted from function.register-shutdown-function by cmb
| From: | cmb@php.net | Date: | Sun, 03 Feb 2019 18:06:57 +0000 |
| Subject: | note 112746 deleted from function.register-shutdown-function by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210185@lists.php.net to get a copy of this message | ||
Note Submitter: ealexs at gmail dot com
----
If you use register_shutdown_function under fastCGI (using php 5.4.4, Apache 2.2/Debian) and the
user clicks cancel request in the browser register_shutdown_function will not be called.
My solution was to use ignore_user_abort(true) to ignore the user abort.
Example (you may test request abort in the browser with and without ignore_user_abort):
<?php
// use this to make sure the shutdown function will be called
ignore_user_abort(true);
$f = fopen("debug.out.txt", "wt");
function qShutdown()
{
global $f;
// use __DIR__ as the shutdown it's not invoke under the same folder
file_put_contents(__DIR__. "/qShutdown.txt", "qShutdown called");
fwrite($f, "I was called\n");
fclose($f);
echo "I was called\n";
}
register_shutdown_function("qShutdown");
// do some random stuff
echo "things were executed\n";
fwrite($f, "things were executed\n");
fflush($f);
$n = 10;
$i = 0;
while ($i < $n)
{
sleep(1);
$i++;
echo "Working {$i} ...\n";
fwrite($f, "Working {$i} ...\n");
fflush($f);
ob_flush();
flush();
ob_flush();
}
fwrite($f, "die called\n");
fflush($f);
die("die called\n");
?>