如何使用 PHP 每秒在浏览器中显示一条消息

How to display a message in the browser every second using PHP

有没有人找到解决方案,如何使用 PHP 在浏览器中每秒显示一条消息?
到目前为止,我一直使用以下代码,它在我的服务器 运行 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>

我看到一些帖子 ob_flush 不再适用于较新的 IIS 版本。
我在 Windows 10.

上使用 IIS 在一些帖子中,我读到将 responseBufferLimit="0" 添加到文件 C:\Windows\System32\inetsrv\config\applicationHost.configPHP_via_FastCGI 部分。

但是在这个文件中我只有以下关于 fastCgi 的部分,所以我不知道如何将 responseBufferLimit="0" 添加到这个部分:

  <fastCgi>
    <application fullPath="C:\Program Files\PHP\php-cgi.exe" />
  </fastCgi>

非常感谢任何帮助。

以下代码绝对不是一个聪明的好解决方案,但也许您可以将其用作解决方法,直到这里有人向我们展示了我也非常感兴趣的智能解决方案。

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

我做了一些测试。它仍然是一种解决方法,但更聪明一些,我认为只要没有更好的解决方案,你就可以接受它。
您只需在代码的最开头添加 echo str_repeat(" ", 7500000);ob_flush()flush() 将执行它们应该执行的操作(也适用于新的 IIS 版本):

<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>