【问题标题】:Error with output buffering and FirePHP输出缓冲和 FirePHP 出错
【发布时间】:2011-09-28 21:27:21
【问题描述】:

在下面的代码中执行“echo ...”的那 2 行中出现无法解释的“标头已在 #...行发送”错误。

案例的简化版:

<?php
ob_start();

//Initializing FirePHP...
include_once(F_FS_PATH."lib/FirePHPCore/fb.php");
// <--- I've also tried to move the ob_start(), after the FirePHP init,
// <--- instead before it. But it made no difference.
?>
<html>
<div>A lots of HTML (and php) code goes here... Actually my entire page.
FirePHP is also used here many times by multiple invocations
of the function fb('debug text');</div>
</html>

<?php
$all_page_content=ob_get_clean();

if ($GLOBALS["marketing_enabled"])
    echo marketingReplaceContent($all_page_content);
else
    echo $all_page_content;

ob_flush(); flush();

//Do some other non-printing - but slow stuff.
do_the_silent_slow_stuff_Now();

// <--- presumably the php execution ends here.
?>

我不明白为什么在我打印缓冲区并刷新它之后,FirePHP 会在页面完成时尝试做某事?或者它在尝试什么?我该如何应对这个问题? :(

【问题讨论】:

  • 你在do_the_silent_slow_stuff_Now()做什么?如果有任何触发 php 的调用,它将尝试发送标头,但这是行不通的,因为您已经使用 flush() 发送了输出。
  • 呵呵 :) 是的。我在 do_the_silent_slow_stuff_Now() 中有一个 fb() 调用。我怎么没想到问题就在那里。很简单!我原以为问题会出现在 echo 语句之前的某个地方,但是看那里是不合逻辑的。无论如何,写这个作为对我问题的回复,我会将其标记为答案。对于像我这样困惑的谷歌用户来说,这可能很有用:)

标签: php debugging firebug output-buffering firephp


【解决方案1】:

这是你的问题:

标头已在第 # 行发送...

当您使用 FirePHP 并预先回显某些内容时会发生什么。这甚至可能是&lt;?php 标记之前的空格。 FirePHP 将其所有内容作为 header 发送,并且在进行任何输出后无法发送 headers。

由于我确定您在 do_the_silent_slow_stuff_Now(); 方法中调用了 FirePHP,因此我建议不要同时使用缓冲、刷新和 FirePHP。

要么在开发阶段退出ob_start()ob_flush(),要么在所有工作完成后调用ob_flush() 方法。

第三种可能性是通过$development = true; 之类的操作将您的开发和实时阶段分开,并制作您自己的 FirePHP 函数:

function my_fb($text) {
    if(!$development)
        fb($text);
}

和:

if($development) {
    do_the_silent_slow_stuff_Now();
    ob_flush(); flush();
}
else {
    ob_flush(); flush();
    do_the_silent_slow_stuff_Now();
}

希望这会有所帮助!

【讨论】:

  • 现在当我完全理解这种行为后,除了您的建议之外,我还会再添加一个建议:添加第二级输出缓冲。
  • @PatlaDJ 很高兴我能帮上忙...我还为我的回答添加了一些细节。
猜你喜欢
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多