问题
For example I have something like this :
<?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();
?>
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.
How to print something when in infinite loop, on my hosting?
Maybe I have to enable /disable something in my php.ini
file? any Ideas?
回答1:
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:
Disable Gzip on the Linux hosting by using either PHP
ini_set('output_buffering','on');
ini_set('zlib.output_compression', 0)
Disable Gzip by using .htaccess
SetEnv no-gzip dont-vary
EDIT:
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.
来源:https://stackoverflow.com/questions/10947908/how-to-echo-something-when-in-infinite-while-loop