无限while循环时如何回声? [英] How to echo something when in infinite while loop?

查看:93
本文介绍了无限while循环时如何回声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这样的东西:

<?php
  header("Content-Type: text/html; charset=UTF-8");
  set_time_limit(0); 
  ob_start("ob_gzhandler");

  while(true) :
    echo microtime(true)."<br>";
    ob_flush();
    flush(); 
    sleep(1);
  endwhile;

  ob_end_clean();
?>

此代码在我的localhost上有效,并且页面上的每一秒都被打印出microtime(),但是当我尝试在我的Shared Linux Hosting上运行相同的脚本时,没有任何内容被打印出来,页面的加载时间无限长.

This code works on my localhost and each second on the page is printed microtime(), but when I try to run same script on my Shared Linux Hosting nothing is printed, page just has infinite loading time.

如何在主机上无限循环打印某些内容?

也许我必须启用/禁用php.ini文件中的某些内容?有任何想法吗?

Maybe I have to enable /disable something in my php.ini file? any Ideas?

推荐答案

Gzip正在等待所有数据,因此它可以压缩并发送.

It's Gzip waiting for all the data, so it can compress and send it.

当您使用共享主机时,完全禁用此功能可能会有些棘手. 因此,我们可以:

As you're on shared hosting, it might be a bit tricky to disable this completely. So we can either:

使用PHP之一在Linux托管上禁用Gzip

ini_set('output_buffering','on');
ini_set('zlib.output_compression', 0)

使用.htaccess禁用Gzip

SetEnv no-gzip dont-vary



编辑: 您还可以在Linux主机上尝试一下吗?



Could you also try this out on your Linux Host?

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

此主机在我的主机上有效,很有趣,看看它是否适用于您的主机.

This one works on my host, it'd be interesting to see if it works on yours.

这篇关于无限while循环时如何回声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆