2

I have a script that used to work in PHP5.3 to handle buffering for a particular log file but after the server was upgraded to PHP5.5 it no longer works. The output needs to be html so I was hoping to simply flush output after each echo.

This is a cut-down test version of the code that used to work...

@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);

set_time_limit(0);

echo 'Start ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo $i . '<br />';
    flush();
    ob_flush();
    sleep(1);
}
echo 'End<br />';

I suspect that the @ini_set commands are not over-riding the settings and I am just hoping for a simple example that will flush the output buffers. Most of the examples online are from 6+ years ago and none of them have worked. I read that buffering was re-written in PHP5.4 so I wonder if that is also to blame.

Das123
  • 855
  • 3
  • 14
  • 26
  • Are you taking into account that some browsers need to see at least 1024 bytes of output before they'll send anything? Flushed or not? – dmgig Jan 08 '15 at 19:26

1 Answers1

1

I have tested your script and done some fixing/Enhancements

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);

// you can dismiss this configuration, the bellow explanation is from the php.ini itself
/* Implicit flush tells PHP to tell the output layer to flush itself
   automatically after every output block.  This is equivalent to calling the
   PHP function flush() after each and every call to print() or echo() and each
   and every HTML block.
*/
@ini_set('implicit_flush', 1); 
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

set_time_limit(0);

echo 'Start ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
    // put the bellow php code if the user browser is Firefox, Internet Explorer or Safari
    // Google Chrome just works fine with it but it do not need
    echo str_repeat(" ", 1024);

    echo $i . '<br />';
    flush();
    // ob_flush(); you have used flush(), why using ob_flush() there is nothing to flush anymore
    sleep(1);
}
echo 'End<br />';

I don't think that the update of PHP version causes the problem, but i am not certain
Hope that helps :)

Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
  • Hmmm. Still doesn't work for me in either FireFox or Chrome. I had read somewhere that @ini-set may not actually work so perhaps I need to set these up in a .htaccess file. Thanks for your response and the excellent commenting. :) – Das123 Jan 08 '15 at 22:55