使用PHP将HTML刷新到浏览器

时间:2013-06-14 18:08:12

标签: php performance flush buffering output-buffering

我正在尝试实现此页面上列出的技巧http://developer.yahoo.com/performance/rules.html#flush“提前刷新缓冲区”。

每当我尝试运行此东西时,我都无法获得所需的输出。

我写了以下代码。

<html>
<head>
    <title>This is title</title>
    <script type="text/javascript" src="/1.js"></script>
    <link rel="stylesheet" type="text/css" href="/1.css">
</head>

<body>
ABC
<?php
flush();
sleep(3);
?>
</body>
</html>

结果enter image description here

我在Firefox和Chrome上都获得了相同的结果。

我期望CSS和JS文件的下载应该立即开始,而不是等待3秒。

根据互联网上提供的信息,我尝试了以下内容,但没有任何帮助。

1. ob_start(); and then ob_flush();

2. Using both ob_flush(); and flush(); ( in both the orders )

3. Adding the thing like this
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);

4. Putting more content in body 4~5 KB of content before flush.

5. And many other things.

我怀疑实现这种事情是否真的可能。

2 个答案:

答案 0 :(得分:0)

嗯...在雅虎的文档中,flush()</head>之后和<body>之前。

  ... <!-- css, js -->
</head>
<?php flush(); ?>
<body>
  ... <!-- content -->

在您的代码中,它位于<body>内。你试过把php代码放在它之前吗?

答案 1 :(得分:0)

嗨我遇到了同样的情况,ob_start和ob_flush本身也不适合我。

所以编辑完代码后就可以了 EDIT ::

<?php 
if (ob_get_level() == 0) ob_start();
for($i = 0 ; $i < 10 ; $i++){

echo $i . '<br>';
sleep(1);
    echo str_pad('',4096) ;   
    ob_flush();
    flush();

}
?>