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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kb1ooo
Forum Newbie
Posts: 2
Joined: Mon Oct 27, 2008 8:11 am

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

Post 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++;
}
 
 
?>
 
kb1ooo
Forum Newbie
Posts: 2
Joined: Mon Oct 27, 2008 8:11 am

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

Post 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
Post Reply