2

I'm using the following code to produce buffered output on a db maintenance script:

function flush_buffers($string){
    echo $string;
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();

}

While this works as expected on my local Wamp server, showing output each time the function is invoked, it doesn't on the online web server: here the output is sent only once the script has ended. How is that?

Riccardo
  • 2,054
  • 6
  • 33
  • 51
  • Does your web server have any additional buffering outside/beyond PHP's control? I believe that IIS, for example, commonly does this. – Wiseguy Jun 17 '11 at 12:37
  • Netcraft reports: "Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 " – Riccardo Jun 17 '11 at 12:41
  • @dqhendricks, flush() manual states that some server overrides could occur :-( @Wiseguy, see second comment on flush() php manual (by user "brudinie") – Riccardo Jun 17 '11 at 12:44

1 Answers1

4

Make sure output buffering is off in your php.ini file on your web server.

You also don't have to flush manually every time, you can make use of:

ob_implicit_flush(true);
ob_end_flush();

You should also remember that this is still browser specific. The browser will decide whether to show the output. Some browsers (for example IE6) won't output anything until it has enough characters to output.

The following will turn off everything that could cause unwanted output buffering.

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
Abs
  • 56,052
  • 101
  • 275
  • 409
  • @Abs, same browser on two servers: one working, one not. Will ask the admin to check the settings in the PHP.ini file. Thanks – Riccardo Jun 17 '11 at 13:02
  • @Riccardo - you can do a quick test.php scrip where you just make use of `phpinfo()` and you can look for the `output_buffering` directive in your browser. – Abs Jun 17 '11 at 13:08
  • @Abs, has no settings: output_buffering "no value, no value" – Riccardo Jun 17 '11 at 13:23
  • @Riccardo and what is it set as for your WAMP server? Btw, there is a great comment in php.net by mandor. I have updated my question with it. – Abs Jun 17 '11 at 13:40
  • Don't use implicit flush, especially on your production site. Implicit flush will cause the output buffer to flush after EVERY output - including any echo, print, print_r, etc. etc. This has some serious overhead ramifications. While its fine for debugging purposes, you should look into a more strategic use of output buffering with your PHP application. Echo often, flush once. – Jarrod Nettles Jun 17 '11 at 13:49
  • @Jarrod - I think this is what Riccardo wants so that his DB maintenance script shows output after every time his function is called. The above code is script/page specific so it won't effect the rest of his application. Your advice is correct, but may not apply in this situation. – Abs Jun 17 '11 at 14:06
  • 1
    @Abs, this is correct. @Jarrod, it used to work on the production server, then due to resources usage limitation, it may have been turned off. – Riccardo Jun 17 '11 at 14:22