1

I'm encountering a problem where certain browsers (through me using multiple computers) seem to process/display PHP differently. Say I have the following loop on test.php

<?PHP
for($i=0;$i<10000;$i++){
   echo "This is loop number $i.<br/>";
}
>?

What I would like to see when I load test.php is for each line of "This is loop number #" to appear one after the other as the PHP is processed.

What I end up seeing on some browsers (mainly firefox on my main computer) is that test.php will load (displaying nothing) and then finally open with all the echo lines already present.

Is there a way to force the browser (or webpage) to display the PHP as it goes forward instead of waiting for it to be completed?

Thanks for any help/advice,

Ronan

Ronan
  • 407
  • 3
  • 5
  • 13
  • I can confirm the issue with firefox and edge, opera and chrome output the content instantly. Check this answer to understand why http://stackoverflow.com/a/6926886/797495 – Pedro Lobito May 07 '16 at 17:13
  • I've taken a look at this, however even using the headers both firefox and chrome still fail to load the page as the PHP is progressing. – Ronan May 07 '16 at 19:42

1 Answers1

0

Quoting a different answer:

The HTML5 charset sniffing buffer size is 512 bytes. So the response needs to either have 512 bytes of padding or set a charset (via tag or HTTP header).


I can confirm the delayed output happens on Firefox and Edge, Chrome and Opera output the content immediately, independent of the buffer size.

The code bellow outputs in real-time on all browsers, as long as the str_pad size is > 1024 (this value can differ from browser to browser)

while (@ob_end_flush());
ob_implicit_flush(true);
echo "first line visible to the browser";
echo str_pad("",1024," "); // try lower values and the "first line" won't be displayed instantly
echo "<br />";
sleep(5);
echo "second line visible to the browser after 5 secs";

php.ini excerpt:

output_buffering

; Output buffering is a mechanism for controlling how much output data ; (excluding headers and cookies) PHP should keep internally before pushing that ; data to the client. If your application's output exceeds this setting, PHP ; will send that data in chunks of roughly the size you specify. ; Turning on this setting and managing its maximum buffer size can yield some ; interesting side-effects depending on your application and web server. ; You may be able to send headers and cookies after you've already sent output ; through print or echo. You also may see performance benefits if your server is ; emitting less packets due to buffered output versus PHP streaming the output ; as it gets it. On production servers, 4096 bytes is a good setting for performance ; reasons. ; Note: Output buffering can also be controlled via Output Buffering Control ; functions. ; Possible Values: ; On = Enabled and buffer is unlimited. (Use with caution) ; Off = Disabled ; Integer = Enables the buffer and sets its maximum size in bytes. ; Default Value: Off ; Development Value: 4096 ; Production Value: 4096 ; http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering

You may want to try

output_buffering = off  

But your server performance may suffer.

Community
  • 1
  • 1
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • I've attempted to both add the header criteria and run the code you gave; both the pages I am looking at, and this test snippet only load after everything is processed. This on Chrome and Firefox. I would like to clarify that this only affects me as an admin, no-one else will ever see these pages; so if there is a client-side 'hack' possible that would work as well. – Ronan May 07 '16 at 17:50