FirePHP Kohana 2 and PHP’s Flush Function

Struggled with this for a few hours today, trying to get the awesome FirePHP working with a custom CMS based on Kohana 2.3 but was getting the following cryptic error message:

Fatal error: Exception thrown without a stack frame in Unknown on line 0

After a bit of Googling and searching of the Kohana forums I found out that I could get a more meaningful error message if I modified my Kohana Exception Handler as the one included with 2.3 is prone to.. exceptions! Ha. So just wrap the exsisting exception_handler() function in /system/core/Kohana.php with the following:

public static function exception_handler($exception, $message = NULL, $file = NULL, $line = NULL)
{
	try
	{
		...
	}
	catch (Exception $e)
	{
		// Clean the output buffer if one exists
		ob_get_level() and ob_clean();

		// This can happen when the kohana error view has a PHP error
		echo $e->getMessage(), ' [ ', $e->getFile(), ', ', $e->getLine(), ' ]';

		// Exit with an error status
		exit(1);
	}
}

Success! A more meaningful error message, unfortunately it’s still an error…

Headers already sent in on line 0. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive. [ DIR, LINE_# ]

Now this error message really got me stumped! Kohana already does it’s own thing with ob_start so I knew this was already running, plus in my php.ini it was enabled too. Strange.

A stroke of luck occurred while I was reading abut the PHP function flush(), in one of the comments someone wrote

Following the speed-up rules from http://developer.yahoo.com/performance/rules.html in the topic “Flush the Buffer Early” with ob_start() will output headers early and thereby corrupt error handling events extended with eg. FirePHP that will trigger something like; “Exception thrown within the exception handler. Message: Headers already sent in on line 0. Cannot send log data to FirePHP.” or “Exception thrown without a stack frame in Unknown on line 0”.

Bingo!! In my template code was a call to flush(), after changing this to ob_flush() after worked correctly!

Hope this helps out anybody have the same issue!