7

We have been using php flush to "blank" a page immediately as soon as it is clicked, and also to send the navigation and main components of the page so that a page appears nearly instantly, even though sometimes the content may take a long time to load.

This has been working very well.

Recently we upgraded from IIS 7.0 to 7.5 and now flush does not work. While investigating the problem we have turned off compression for both static and dynamic files. We have also turned off output caching.

We also have zlib compression turned off and output buffering off in php.ini.

In order to test the problem we have the following script

@ini_set("output_buffering", "Off");
@ini_set('implicit_flush', 1);
@ini_set('zlib.output_compression', 0);

ob_start();

echo "starting...<br/>\n";
for($i = 0; $i < 5; $i++) {
    print "$i<br/>\n";
    ob_end_flush(); 
    ob_flush();
    flush();
    ob_start();
    sleep(2);
}
print "DONE!<br/>\n";

The browser just shows the loading status (whatever that is in any browser, in IE it looks like an Ajax animated gif, in Firefox the tab will say "Connecting...") for 10 seconds, and then suddenly the entire output appears.

We have tried various combinations of flush and ob_flush and ob_end_flush based upon similar questions on this site. None of them work. Is there any way to make IIS/PHP flush the data?

Jeff Davis
  • 4,736
  • 4
  • 38
  • 44

7 Answers7

38

There is another way to set the Response Limit using the IIS Manager:

  1. On the server main page, under "Management", select "Configuration Editor";
  2. under "Section", enter 'system.webServer/handlers';
  3. next to "(Collection)" click "..." OR mark the element "(Collection)" and, under "Actions" und '(Collection)' Element, click "Edit Items";
  4. scroll down until you find your PHP version under "Name";
  5. at the bottom, the Properties are shown an can be edited manually, including responseBufferLimit, which should be set to 0 for flush() to work.

The big Pro is that you can edit the properties for everything, not only PHP plus you can work with different versions (or even installations of the same version) of PHP.

HTH

Dario
  • 381
  • 3
  • 2
7

You must set the ResponseBufferLimit value of the desired handler to a number low enough to actually flush. I recommend using 0 since it prevents IIS from doing anything but passing along what you send it from your PHP script. You can use the following command line to set the ResponseBufferLimit to 0 for the php handler (just change “NAME” to the name of the handler you want to update e.g. PHP53_via_FastCGI):

appcmd.exe set config /section:handlers "/[name='NAME'].ResponseBufferLimit:0"

Alternatively, you can edit the applicationHost.config directly and add a ResponseBufferLimit attribute the XML element.

artlung
  • 33,305
  • 16
  • 69
  • 121
Justin
  • 541
  • 7
  • 15
4

Enter the following command as Administrator in Powershell:

C:\Windows\System32\inetsrv> .\appcmd.exe set config /section:handlers "/[name='PHP_via_FastCGI'].ResponseBufferLimit:0"

Expected Output:

Applied configuration changes to section "system.webServer/handlers" for "MACHINE/WEBROOT/APPHOST" at configuration comm it path "MACHINE/WEBROOT/APPHOST"

For more background, have a look at: http://www.coastrd.com/cgioniis7

Basically, we need to tell FastCGI to turn of its ResponseBufferLimit. This cannot be done through the IIS Management Console (checked only 7.5)

artlung
  • 33,305
  • 16
  • 69
  • 121
knatsch
  • 41
  • 1
4

What I do is that I use the following function:

function flush_buffers(){
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}

So in your code:

ob_start();
flush_buffers();

echo "starting...<br/>\n";
for($i = 0; $i < 5; $i++) {
    print "$i<br/>\n";
    flush_buffers();
    sleep(2);
}

It should work flawlessly :-)


Here is some working code (with correct Content-Type set):

<?php
header("Content-Type: text/html; charset=utf-8");
function flush_buffers(){
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}

ob_start();
flush_buffers();
echo "starting...<br/>\n";
for($i = 0; $i < 60; $i++) {
    flush_buffers();
    print   "$i<br/>\n";
    flush_buffers();
    sleep(2);
}

flush_buffers();

print "DONE!<br/>\n";
?>
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • @Jeff, the demo should help a bit :-) – Naftali Aug 24 '11 at 16:18
  • 1
    It looks like the key is the content-type: text/html; charset=utf-8. Adding this as a header works too. In particular it looks like the charset=utf-8 is what makes it work. – Jeff Davis Aug 24 '11 at 16:23
  • @Jeff, that seems to be true, i changed my demo to reflect that and im changing my answer a bit now. – Naftali Aug 24 '11 at 16:26
  • @Jeff -- i cleaned my answer a bit. – Naftali Aug 24 '11 at 16:31
  • Scratch that. This change only works when you connect locally on the server. When connecting from another machine it still doesn't work. A step in the right direction, however. – Jeff Davis Aug 24 '11 at 16:35
  • Your demo page (on blipit) doesn't work for me. Trying in IE9/FF7Beta/Chrome14Beta. All of them wait 60 seconds before showing anything. – Jeff Davis Aug 24 '11 at 16:47
  • @JeffDavis let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2811/discussion-between-neal-and-jeff-davis) – Naftali Aug 24 '11 at 16:49
  • It times out on proxify as well, so I don't think it's just my computer or location. – Jeff Davis Aug 24 '11 at 16:50
  • @Jeff -- Oh! add `set_time_limit(0);` to the top of your php file, that should fix the timeout. – Naftali Aug 24 '11 at 16:52
2

I'm a little late to the party, but thought I'd add how to do this with web.config.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
        <!--- other stuff here --->
        <handlers>
            <remove name="ISAPI-dll" />
            <add name="ISAPI-dll" path="*.dll" verb="*" type="" modules="IsapiModule" scriptProcessor="" resourceType="File" requireAccess="Execute" allowPathInfo="true" preCondition="" responseBufferLimit="0" />
        </handlers>
    </system.webServer>
</configuration>
Jules
  • 1,941
  • 15
  • 18
1

It's up to the webserver whether it decides to naggle the content or send it via chunked encoding. So although PHP can ask the server to push data out to the client, it can't force the server to use chunked encoding.

This article suggests you explicitly need to set the transfer encoding for IIS (see the bit about ISAPI) for sending data to the server - you might try the same in your script.

IME, most scenarios where this is an issue can be better dealt with by....

register_shutdown_function('do_slow_stuff');
....generate html content....
exit; // closes stdin/stdout, but shutdown fn will still be called 

function do_slow_stuff()
{
  ....
} 
symcbean
  • 47,736
  • 6
  • 59
  • 94
0

Here's another way of doing this with web.config (@Jules's method didn't work for me with IIS 8.0). Of course, you would want to replace the PHP versions and paths with the ones actually on your machine.

This allows the usage of server-sent events!

<configuration>
    <system.webServer>
        <handlers>
            <remove name="PHP53_via_FastCGI" />
            <remove name="PHP54_via_FastCGI" />
            <remove name="PHP55_via_FastCGI" />
            <add name="PHP53_via_FastCGI" path="*.php" verb="GET,HEAD,POST" type="" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\PHP\v5.3\php-cgi.exe" resourceType="Either" requireAccess="Script" allowPathInfo="true" preCondition="" responseBufferLimit="0" />
            <add name="PHP54_via_FastCGI" path="*.php" verb="GET,HEAD,POST" type="" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\PHP\v5.4\php-cgi.exe" resourceType="Either" requireAccess="Script" allowPathInfo="true" preCondition="" responseBufferLimit="0" />
            <add name="PHP55_via_FastCGI" path="*.php" verb="GET,HEAD,POST" type="" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\PHP\v5.5\php-cgi.exe" resourceType="Either" requireAccess="Script" allowPathInfo="true" preCondition="" responseBufferLimit="0" />
        </handlers>
    </system.webServer>
 </configuration>
Mike Godin
  • 3,727
  • 3
  • 27
  • 29