Search code examples
phpdebuggingfirebugoutput-bufferingfirephp

Error with output buffering and FirePHP


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? :(


Solution

  • 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!