Php connection status and connection handling

by Rekha 2009-12-07 13:57:21

PHP maintains a connection status bitfield with 3 bits:
o 0 - NORMAL
o 1 - ABORTED
o 2 - TIMEOUT
By default a PHP script is terminated when the connection to the client is broken and the ABORTED
bit is turned on. This can be changed using the ignore_user_abort() function. The TIMEOUT bit is
set when the script timelimit is exceed. This timelimit can be set using set_time_limit().

ignore_user_abort() function By default the php script is aborted when client stops or disconneted. Sometime it is required to always have the scripts run to completion. This function sets whether a client disconnect should cause a script to be aborted. It will return the previous setting and can be called without an argument to not change the current setting and only eturn the current setting. This behaviour can also be set via the ignore_user_abort php.ini directive as well as through the corresponding "php_value ignore_user_abort" Apache .conf directive.

register_shutdown_function(). By default the php script is aborted when client stops or disconneted. One exception to this is if you have registered a shutdown function using register_shutdown_function(). This registered shutdown function will get called when the script is aborted by client disconnect. Note that this registered shutdown function will get called even if the script is terminating normally.

connection_aborted() function. This function returns TRUE if client disconnected. Typically this function is useful in registered shutdown functions to do something different in case of a client disconnect and php connection status is ABORTED.

Controling script execution time set_time_limit() function. The default timeout for any php script is 30 seconds. This can be changed using the max_execution_time php.ini directive or the corresponding "php_value max_execution_time" Apache .conf directive. This can also be controlled run time by using set_time_limit() function. This function sets the number of seconds a script is allowed to run. If seconds is set to zero, no time limit is imposed. When called, set_time_limit() restarts the timeout counter from zero. i.e if the timeout is the default 30 seconds, and after 25 seconds into script execution a call set_time_limit(20) is made, the script will allow to run for a total of 45 seconds before timing out.

connection_timeout() function. This function returns TRUE if php connection status is TIMEOUT. Typically this function is useful in registered shutdown functions to do something different in case of php connection status is TIMEOUT.

connection_status() function. Note that both the ABORTED and the TIMEOUT states can be active at the same time. This is possible when ignore_user_abort is set and client disconnected. At this time script will keep running but PHP will still note that it is ABORTED. If it then TIMEOUT it will stop execution of the script and registered shutdown function will be called if registered. At this point connection_timeout() and connection_aborted() will return TRUE. connection_status() can also be used which returns the connection status bitfield. So, if both states are active it would return 3.

Example:
<?php
set_time_limit(0);
ignore_user_abort(true);
/* code which will always run to completion */
?>

You can call connection_status() to check on the status of a connection.


<?php
ignore_user_abort(true);
echo "some output";
if(connection_status()==0) {
// Code that only runs when the connection is still alive
} else {
// Code that only runs on an abort
}
?>

You can also register a function which will be called at the end of the script no matter how the script
was terminated.

<?php
function foo() {
if(connection_status() & 1)
error_log("Connection Aborted",0);
if(connection_status() & 2)
error_log("Connection Timed Out",0);
if(!connection_status())
error_log("Normal Exit",0);
}
register_shutdown_function('foo');
?>

Tagged in:

2120
like
0
dislike
0
mail
flag

You must LOGIN to add comments