2

I get unexplained "Headers already sent on line #..." error on those 2 lines that execute "echo ..." in the code below.

Simplified version of the case:

<?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.
?>

I cannot understand why FirePHP is trying to do something upon page completion after I print the buffer and flushed it? Or what is it trying? How can I cope with this problem? :(

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
PatlaDJ
  • 1,226
  • 2
  • 17
  • 31
  • 1
    what do you do in the `do_the_silent_slow_stuff_Now()`? If there is any call to fire php, it will try to send headers, which won't work, since you already sent output with `flush()`. – Sascha Galley Jul 05 '11 at 14:44
  • hehe :) Yes. I had one fb() call there in do_the_silent_slow_stuff_Now(). How it didn't occurre to me that the problem was there. So simple! I was expecting the problem to be somewhere before the echo statements, but is so illogical to look there. Anyway, write this as reply to my question, I will mark it as answer. It may be usefull for some googler baffled like me :) – PatlaDJ Jul 05 '11 at 14:53

1 Answers1

3

Here's your problem:

Headers already sent on line #...

Thats exaclty what happens when you use FirePHP and echo something beforehand. This might even be a whitespace before the <?php tag. FirePHP sends all its content as a header, and headers can't be send after any output is made.

Since I'm sure that you call FirePHP in your do_the_silent_slow_stuff_Now(); method I recommend not to use buffering, flushing and FirePHP at once.

Either you resign on ob_start() and ob_flush() during the development phase or you call the ob_flush() method after all stuff is done.

Third possibility would be to seperate your development and live phase by doing something like $development = true; and make your own FirePHP function:

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

and:

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

Hope this helps!

Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
  • Now when I fully understand this behaviour, in addition to your recommendation I will also add one more recommendation:adding second level of output buffering. – PatlaDJ Jul 05 '11 at 14:56
  • @PatlaDJ glad I could help...I also added some details to my answer. – Sascha Galley Jul 05 '11 at 14:57