douci2015 2012-06-28 20:26
浏览 102
已采纳

无法在Windows中使用flush()

We have a ubuntu test server that this simple script works fine on where it outputs a number each second to the browser

<?
    for ($i=0; $i<3; $i++) 
    {
        echo $i.'<br>';
        flush();
        sleep(1);
    }

However on my local development box (windows environment) I cannot get it to work, it outputs it all after 3 seconds.

I am using the same php version (5.3.13) as well as apache (2.2.22) version on my local box as the test server. I have mirrored most of the settings in both php.ini as well as the httpd.conf Here are settings I have set (which are identical to the test server)

output_buffering 0
zlib.output_compression = Off
zlib.output_compression_level = -1

I don't have mod_gzip installed.

I have even tried doing a wamp install on my box, and it didn't work with that either.

I can't imagine it being a client (browser -- firefox 13) issue as I use the exact same browser to view the script on the test server and it works fine

I have tried enabling implicit flush, doing ob_flush, etc. No luck.

Anyone have any ideas on how to get this working?

  • 写回答

2条回答 默认 最新

  • doujiebo9849 2012-06-28 21:41
    关注

    Just keep in mind what @Wrikken said, this is not recommended.

    So I was thinking, everything you're doing seems to be right. I copied your setup and tried. I faced same problem. I executed the script from command line, it worked.

    Then I had to do the last test, Wireshark. Following the first couple of packets it was clear that the server is sending everything correctly, so it had to be the browser’s buffer.

    So I tried send some data before the loop, well guess what? It worked!

    Here you go:

    <?php
    ini_set('output_buffering','off');
    ini_set('zlib.output_compression', 0);
    echo str_repeat(" ", 1024), "
    ";
    for($i=0;$i<6;$i++) {
          echo $i."<br />
    ";
          ob_flush();
          flush();
          sleep(1);
    }
    ?>
    

    I'm not sure about the application you have in mind, but this is clearly not a good idea, I highly recommend that you look into other options.

    Update: After a lengthy Google search I found this and this

    Because a browser cannot correctly render a page without knowing how to construct the page's characters, most browsers buffer a certain number of bytes before executing any JavaScript or drawing the page

    And

    To avoid these delays, you should always specify the character encoding as early as possible, for any HTML document larger than 1 KB (1024 bytes, to be precise, which is the maximum buffer limit used by any of the browsers we have tested).

    So I tried to send the charset first instead of filling the buffer: (and it worked)

    <?php
    ini_set('output_buffering','off');
    ini_set('zlib.output_compression', 0);
    //echo str_repeat(" ", 1024), "
    ";
    header('Content-Type: text/html; charset=iso-8859-1');
    //Note that it shoudn't matter which charset you send
    for($i=0;$i<6;$i++) {
          echo $i."<br />
    ";
          ob_flush();
          flush();
          sleep(1);
    }
    ?>
    

    So why was it working with the first server but not the second?

    Most likely it's because your first server is sending the charset with the header while the second one isn't.

    In your case, however, I'd make the following changes

    <?php
    ini_set('output_buffering','off');
    ini_set('zlib.output_compression', 0);
    //Plain text MIME type since you'll use for logging purposes
    //and if you run it from CLI, you can ignore the whole header line
    header('Content-Type: text/plain; charset=iso-8859-1');
    for($i=0;$i<6;$i++) {
          //No need to echo <br /> once you'll run it from CLI
          echo $i."
    ";
          ob_flush();
          flush();
          sleep(1);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试