View Single Post
  #12 (permalink)  
Old 12-18-2007, 06:19 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: PHP Tips and Tricks

Connection Handling

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().

<?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');
?>
__________________
Thanks & Regards
Sabari...
Reply With Quote