在 PHP 中,flush() 不显示输出?
我有这样的代码:
set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
ob_flush();
flush();
$start = time();
$secs = time() - $start;
while ($secs <= 300)
{
echo "this script has been running for $secs seconds.\n";
ob_flush();
flush();
sleep(1);
}
当我查看此页面时,我想做的是实时查看脚本运行了多长时间,如下所示:
- 脚本已运行 1 秒。
- 脚本已运行 2 秒。
- ............
- 脚本已运行 300 秒。
相反,我得到的是一个空白窗口,其中有 5 分钟的连续“正在加载”标志,5 分钟后突然我被大量这些消息轰炸,而我应该一次收到 1 条消息。
有人可以解释我做错了什么吗?
I have this code:
set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
ob_flush();
flush();
$start = time();
$secs = time() - $start;
while ($secs <= 300)
{
echo "this script has been running for $secs seconds.\n";
ob_flush();
flush();
sleep(1);
}
What I'd like to do when I view this page, is to see in real time how long the script has been running, like this:
- Script has been running 1 seconds.
- Script has been running 2 seconds.
- ............
- Script has been running 300 seconds.
Instead what I get is a blank window with a continuous 'loading' sign for 5 minutes, and after 5 mins suddenly i'm bombarded with a load of these messages which i should've been getting 1 message at a time.
Can someone explain what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ob_flush
不是flush
。 ob_flush 清除已打开的对象缓冲区。由于您没有打开对象缓冲区,因此不会刷新任何内容。此外,网络浏览器和网络服务器软件因在输出数据之前保留数据而臭名昭著。确保 GZIPing 已关闭并且您使用的是正常的浏览器。
ob_flush
is notflush
.ob_flush
clears the object buffer that's been opened. Since you don't have an object buffer open, nothing is flushed.Also, web browsers and web server software are notorious for holding up data until it can be outputted. Make sure GZIPing is turned off and that you're using a sane browser.