dsjfrkvn818747 2014-03-22 19:07
浏览 64

为什么flush()不能用于for循环? (PHP)

I want use flush() function in for loop that echo content after each loop
my code is:

<?php
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<10; $i++){
        echo "<br> Line".$i." to show.";
        ob_flush();
        flush();
        sleep(2);
}

echo "<br>Done.";

ob_end_flush();
?>

this code works on localhost and another host(Demo) but not working on my main host(Demo + phpinfo page)

how i can solve this? I use chrome

  • 写回答

3条回答 默认 最新

  • doujia1939 2014-03-22 19:23
    关注

    There's a »varnish« proxy in front of your second server. It disturbs the timing of your script. You should generally not rely on timing when returning your response, precisely because there might be a proxyserver in the way. You might want to use some sort of polling or WebSockets instead.

    $ nc shoma.info 80
    GET /test/1.php HTTP/1.1
    Host: shoma.info
    Connection: close
    
    HTTP/1.1 200 OK
    Server: Apache
    X-Powered-By: PHP/5.3.28
    Vary: Accept-Encoding
    Content-Type: text/html
    Content-Length: 209
    Accept-Ranges: bytes
    Date: Sat, 22 Mar 2014 19:18:40 GMT
    X-Varnish: 972965439
    Age: 0
    Via: 1.1 varnish <------- this is the problem
    Connection: close
    
    <br> Line 0 to show.<br> Line 1 to show.<br> Line 2 to show.<br> Line 3 to show.<br> Line 4 to show.<br> Line 5 to show.<br> Line 6 to show.<br> Line 7 to show.<br> Line 8 to show.<br> Line 9 to show.<br>Done.
    

    BTW: My firefox here doesn't even show the »animation« of your first server. So you can't even rely on the browser displaying it.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?