note 42774 added to function.register-shutdown-function
| From: | php at kevin dot offwhite dot net | Date: | Fri, 28 May 2004 18:14:10 +0000 |
| Subject: | note 42774 added to function.register-shutdown-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70249@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.register-shutdown-function.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42774
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42774&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42774&report=yes
Search -- http://master.php.net/manage/user-notes.php