Post Processing Limitations in PHP

By Angsuman Chakraborty, Gaea News Network
Sunday, September 18, 2005

In PHP you can register a function to be executed when script processing is complete using register_shutdown_function.

In PHP 4.0.6 and below the registered shutdown functions are called after the request has been completed, including sending any output buffers. So a long running function can execute without affecting user experience.

However in PHP 4.1 and above the functions specified in the shutdown hook do not execute after output is closed. The upside is that you can send an echo or print in shutdown hook.

The big downside is that you cannot use this to execute your long-running (post-processing) functions.

It will negatively affect user experience. Moreover cached output may not be displayed unless this function returns.

To minimize user impact it is suggested that flush() is called as the first statement of shutdown function(s). That will ensure that the output is displayed on some modern browsers like Firefox.

Additionally the function processing may timeout after 30 second which is the default max_execution_time in PHP.

Fatal error: Maximum execution time of 30 seconds exceeded in E:\Program Files\Apache Group\Apache2\htdocs\shuttest.php on line 8

It can however be increased with a call as follows:
set_time_limit(120); // This add 2 minutes from now before the script times out
Note: This doesn’t work in safe mode. In safe mode modifying php.ini is the only choice.

BTW: The simplest way to register a shutdown function is:

register_shutdown_function('long_running_function_name');

Note: I have created a new category Tech Note to add short technical notes for future reference for myself and hopefully others. This is my first addition to Tech Note category.

Filed under: How To, PHP, Tech Note, Web
YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :