header("Content-type: multipart/x-mixed-replace;boundary=endofsection");
print "--endofsection\n";
$pmt = array("-", "\\", "|", "/" );
for( $i = 0; $i <10;$i ++ )
{
        sleep(1);
        print "Content-type: text/plain\n\n";
        print "Part $i     ".$pmt[$i % 4];
        print "--endofsection\n";
        ob_flush(); //强制将缓存区的内容输出
        flush(); //强制将缓冲区的内容发送给客户端
}
print "Content-type: text/plain\n\n";
print "The end\n";
print "–endofsection–\n";像手册评论里的这段代码,有两个疑问1.第8行Part $i那里,我换成html代码进行输出,浏览器直接输出了html字符串,而没有对html进行解析2.我不想第7行的"Content-type: text/plain\n\n"和第9行的print "--endofsection\n";输出到浏览器上,但是去掉这两行又实现不了推送了,应该怎么办?

解决方案 »

  1.   

    text/plain 是纯文本啊
    text/html 才是超文本
      

  2.   


    我把print "Content-type: text/plain\n\n"; 改成 print "Content-type: text/html\n\n"; 后确实是解析了html元素,但是浏览器的行为变成等循环结束以后一起把数据输出,而不是像改动前的一句一句不断输出
      

  3.   


    chorme,现在的情况就是print "Content-type: text/plain\n\n"; 时正常推送print "Content-type: text/html\n\n";  时html元素解析正常,但要等到延迟循环结束后一起输出除了这地方,其他代码都没改动
      

  4.   

    我在firefox下试了一下text/html是可以的。
      

  5.   

    您的代码的出处中就明确的声明了这段代码只在 firefox 中有效
    所以你用其他浏览器测试时,出现问题是正常的既然大多浏览器制造商都不愿支持,那么你也没有必要吧精力耗费在这个上面
    要做的事情很多很多
      

  6.   


    header("Content-type: multipart/x-mixed-replace;boundary=endofsection");
    print "--endofsection\n";//这里确保输出大于缓冲区大小长度的内容,str_repeat()函数, 各浏览器通用长度为:1024, 然后就能达到你想要的效果.
      

  7.   


    不错,html能正确解析并推送了。我不想第7行的"Content-type: text/plain\n\n"输出能否做到?我去掉这行就不能正常推送了