FirePHP is a great tool for debugging PHP, and maintaining some level of control over your error handling. Also, it's a great tool for getting information about your variables, objects, etc. while your PHP code is executing. FirePHP requires only a small amount of setup and can be reused time and time again without tedious setups for each site on your server. This tutorial/example requires Firefox.
- Install the Firebug add-on for Firefox
http://getfirebug.com/
- Install the FirePHP add-on for Firefox
https://addons.mozilla.org/en-US/firefox/addon/6149
- Download the FirePHP class
http://www.firephp.org/HQ/Install.htm
- Unzip the FirePHP class, then move the contents of the lib/FirePHPCore directory into a folder on you webserver. If this folder is accessible to all of your websites on your server, you'll be able to use this one FirePHP codebase for all of your sites. If you cannot "reuse" a folder like this, you'll just need to have the FirePHP code base in each site with which you want to use FirePHP. In the examples below, I'm using a path that's always available to me: /Users/adam/Sites/_includes/firephp/FirePHP.class.php. The bold part of the path is a directory that contains the files that were in lib/FirePHPCore.
- Create an include file you reuse in each site
<?php
/**
* FirePHP setup.
* This code should only be found ONCE on your site, Add it to an include file.
* If site has single index.php (Magento, Drupal, etc.), call the include file once (there)
* using require_once(...);
*
* If you need this to be global, please replace $firephp with $GLOBALS['firephp'] everywhere!
* so: $GLOBALS['firebug'] = FirePHP::getInstance(true);
* and in your php code: $GLOBALS['firephp']->log('test of global firephp');
*/
require_once('/Users/adam/Sites/_includes/firephp/FirePHP.class.php');
$firephp = FirePHP::getInstance(true); //non-global
// Add allowed ip addresses below, comma-separated
$allowedIpArr = array("127.0.0.1", "XX.XX.XX.XX");
if(in_array($_SERVER['REMOTE_ADDR'], $allowedIpArr)){
$firephp->setEnabled(true);
}else{
$firephp->setEnabled(false);
}
// Log all errors, exceptions, and assertion errors to Firebug
$firephp->registerErrorHandler($throwErrorExceptions=true);
$firephp->registerExceptionHandler();
$firephp->registerAssertionHandler(
$convertAssertionErrorsToExceptions=true,
$throwAssertionExceptions=false);
?>
- Create a PHP file to test FirePHP
<?php
// Load Firephp from the include file in this site's root
require_once($_SERVER['DOCUMENT_ROOT'] . "/firephp_include.php");
// Make sure we know FirePHP is running by logging a message to the FirePHP
$firephp->info('Debugging using FirePHP');
$myvar1 = array(
array('test' => 'test1-value'),
array('test2' => 'test2-value'),
);
$firephp->fb($myvar1);
$firephp->log('This is a normal log message');
$firephp->warn('This is a warning message', 'My Warning Label');
$firephp->error('This is a error message');
?>
If you have any problems, try the following. If you still have problems, Google It! :)
- Start output buffering using ob_start(); (if output_buffering isn’t set to ‘on’ in your php.ini, .htaccess, etc.)
- Enable the Firebug NET and CONSOLE panels
- Check file permissions; make sure all of the files you're accessing are readable by the web server
I've only shown a few of the possibilities in the example above. Please consult the FirePHP usage page for more features, and a much more thorough explaination of things.
The image below shows the output of the example code above.