skip to Main Content

Has anyone found a solution, how to display a message in the browser every second using PHP?
Until now, I’ve always used the following code, which worked fine on my server running IIS 6:

<html>
  <body>
    <?php
      for ($counter = 0; $counter <= 10; $counter++) {
        echo "Message after " . $counter . " second(s)<br>";
        ob_flush();
        flush();
        sleep(1);
      }
    ?> 
  </body>
</html>

I’ve seen several posts that ob_flush no longer works with the newer IIS versions.
I use IIS on Windows 10.

In some posts I read to add responseBufferLimit="0" into the section PHP_via_FastCGI of the file C:WindowsSystem32inetsrvconfigapplicationHost.config.

But in this file I’ve only the following section about fastCgi so I’ve no idea how to add responseBufferLimit="0" into this section:

  <fastCgi>
    <application fullPath="C:Program FilesPHPphp-cgi.exe" />
  </fastCgi>

Any help is very appreciated.

2

Answers


  1. The following code is definitely not a smart and good solution, but maybe you can use it as a workaround until someone here shows us the smart solution, which I’m also very interested in.

    <html>
      <body>
        <?php
          for ($counter = 0; $counter <= 10; $counter++) {
            echo "Message after " . $counter . " second(s)";
            echo str_repeat(" ", 10000000) . "<br>";
            sleep(1);
          }
        ?> 
      </body>
    </html>
    
    Login or Signup to reply.
  2. I’ve done some tests. It’s still a workaround, but a bit smarter and I think as long as there’s no better solution, you can live with it.
    You just have to add echo str_repeat(" ", 7500000); at the very beginning of your code and ob_flush() and flush() will do what they’re supposed to do (also with the new IIS versions):

    <html>
      <body>
        <?php
          echo str_repeat(" ", 7500000);
    
          for ($counter = 0; $counter <= 10; $counter++) {
            echo "Message after " . $counter . " second(s)<br>";
            ob_flush();
            flush();
            sleep(1);
          }
        ?> 
      </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search