Page 1 of 1

ob_flush() + flush() crashes php on cancel of browser load

Posted: Mon Oct 27, 2008 8:37 am
by kb1ooo
In order to flush ouput to the browser, we need to use both ob_flush() and flush() (Apache 2, windows xp, php 5.2.6). When someone cancels loading of a php page (e.g. back button) Apache should not terminate the script. However, we find that if the script has both ob_flush() and flush() calls, it will get terminated. I'm not sure if it's Apache or php that's doing the terminating. I'm able to replicate this with Apache2 and php 5.2.6 on OS X Leopard as well. Below is a simple script that reproduces the problem. If you hit the script and then back button, the script only write the first output file and then terminate. Take out either the ob_flush() or the flush() and the script will run through as it should:

Code: Select all

<?php
 
$i = 0;
 
while($i < 4)
{
 sleep(4);
 
 echo $i;
 
 $handle = fopen('/tmp/out'.$i.'.txt','w');
 fwrite($handle,'1');
 fclose($handle);
 
 ob_flush();
 flush();
 
 $i++;
}
 
 
?>
 

Re: ob_flush() + flush() crashes php on cancel of browser load

Posted: Mon Oct 27, 2008 1:54 pm
by kb1ooo
ok figured it out in case anyone is interested. I needed either ignore_user_abort(TRUE) at the top of the script or its corresponding php.ini directive.
See
http://us2.php.net/manual/en/features.c ... ndling.php

Thanks,
Marc