ob_flush,flush无效

想要先输出一段再输出一段,网上说如下的可以。

<?php

for ($i=10; $i>0; $i--)
{
 echo $i;
 ob_flush();
 flush();
 sleep(1);
}
?>

但是实际上浏览器没有按照预想的来,还是一次等待加载完。

其他各种办法例如:

<?php
ini_set('output_buffering','on');
ini_set('zlib.output_compression', 0);
ob_implicit_flush();
for($i=0;$i<10;$i++) {
      echo $i;
      echo str_repeat(" ", 500);
      ob_flush();
      flush();
      sleep(1);
}
?>

并修改.htaccess gzip设置 http://www.absolutelytech.com/2010/01/31/solved-flush-ob_flush-not-working-in-php-disabling-gzip-through-htaccess/

SetEnv no-gzip dont-vary

照样不行。

其实php文档已经说得很明白了,看flush函数说明,已经讲了包含服务器设置,浏览器缓存等等问题:

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 </table> 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.

因此,想通过这样的方式达到分步输出是不太靠谱的。


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1