note 42774 deleted from function.register-shutdown-function by cmb
| From: | cmb@php.net | Date: | Sun, 03 Feb 2019 18:06:37 +0000 |
| Subject: | note 42774 deleted from function.register-shutdown-function by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210184@lists.php.net to get a copy of this message | ||
Note Submitter: php at kevin dot offwhite dot net
----
Various commenters have described how this function doesn't work for time-consuming background
processing, because the webpage doesn't close the connection. The workaround using output
buffering and header('Connection: close'); seems to work, but the solution I prefer is to
put the background processing in a separate script which gets included as a transparent gif.
<?php
//logger.php -- does the time-consuming log_it() function
function log_it()
{
sleep(12); //simulate long processing
error_log('log_it() completed');
}
error_reporting(0);
register_shutdown_function('log_it');
header('Cache-Control: no-cache');
header('Content-type: image/gif');
//the size of the unencoded transparent gif
header('Content-length: 85');
//1x1 pixel transparent gif
print base64_decode('R0lGODlhAQABALMAAAAAAIAAAACAA'.
'ICAAAAAgIAAgACAgMDAwICAgP8AAA'.
'D/AP//AAAA//8A/wD//wBiZCH5BAE'.
'AAA8ALAAAAAABAAEAAAQC8EUAOw==');
flush();
exit;
?>
@-----------Page you want to add background processing to ---------@
<html>
<body>
<h1>Hello World</h1>
<!-- Call logger.php to do background processing -->
<img src="logger.php" width="1" height="1" border="0"
alt="" />
</body>
</html>
@------------------------------------------------------------------@
The only caveat is, of course, if the user has images turned off then the script won't be
called. Tested successfully with IE6, Firefox 0.8, and Opera 7.11