Home » Php » http – PHP flush() apparently works on my Linux machine but not on my windows machine?

http – PHP flush() apparently works on my Linux machine but not on my windows machine?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to become familiar with PHP’s flush() function for a project I’m doing, I wrote a few scripts myself, and uploaded them to some webspace I own; but it seemed none of them worked. I picked up the one below from a comment on PHP.net and tried it:

<?php
header( 'Content-type: text/html; charset=utf-8' );

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

Apparently the script STILL wasn’t working.

I then read a comment that said:

If you call ob_flush() and flush() and still dont get the buffer flushed it might be because some antivirus software (Panda in this case) holds the buffer until the page has finished loaded before sending it to the browser.

So I decided to request the webpage on a machine I have using Linux (Ubuntu 12.04) as that doesn’t have any anti-virus software installed; and it worked perfectly!

I have no idea if this is to do with anti-virus software? Or some other strange mechanism in Windows that is preventing flush() from achieving it’s intended purpose, I’m using the same browser on both machines (Firefox 14.0.1) so I doubt it can be that.

So I guess my real question is: Does anybody know anything about anti-virus software preventing flush() from achieving it’s intended purpose (because I can’t seem to find anything about it online) ? And if so, is there any way to get around it?

EDIT: Just to clear up, I’m positive this is NOT server side, I have an external web server running Linux, I am not hosting these scripts locally on either the Linux or Windows Machine.

EDIT2: I tried this around a friends house on the Linux Laptop that I managed to get it working on at my house, strangely the flush code also didn’t work at their house (instead it just took ages to load and all came out at once) which is strange as it works on this laptop in my house, I’m not sure if routers have anything to do with the flush() function working (I really can’t find anything about this on the web) because that is the only thing I can think of that could interfere with it.

Otherwise I’ve made no progress trying to get around this on my own, this is definitely not to do with the server end, which is strange as most information I seem to find is talking about the server being the issue when the flush() function isn’t working :/

Coincidentally I also tried this on a PC running Norton (instead of AVG) at my house, it worked but not quite 100%.

How to&Answers:

There are several things that can prevent output from being immediately displayed, including:

  • Buffering from within PHP (which is what flush prevents)
  • Buffering in order to accumulate enough data to compress (when compression is enabled)
  • Buffering at the web server (it may decide to do so for whatever reason)
  • Buffering inside the browser (browsers use various techniques so that page loading “feels fast” to the end user; these techniques include not always displaying content as soon as it is available)

Since flush cannot control everything on this list, there really is no guarantee that your program will behave as you expect it to.

All of this is mentioned in the notes for flush:

Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the
way to the browser with a few caveats.

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the
browser. It also doesn’t affect PHP’s userspace output buffering
mechanism. This means you will have to call both ob_flush() and
flush() to flush the ob output buffers if you are using those.

Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results
to the browser.

Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent
immediately to the client.

Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line
or the beginning of a tag, and it won’t render tables until the
tag of the outermost table is seen.

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you
may need to send extra whitespace before flushing to get those
browsers to display the page.