Thursday, December 3, 2009

Debugging PHP apps

A major issue I faced in Ajax Development was with debugging the Ajax Request and the Response. A raw method I used for debugging was adding alerts, checking the return value and guessing what maybe going on with the Ajax request until I came across FireBug. Then I started being more wise and started dumping logs and info pertaining to Ajax Requests and Responses using JSON. While using FireBug (a mozilla add on) I stumbled upon FirePHP and that improved my life. So what is FirePHP all about?


FirePHP is an add-on for an add-on: it extends the popular in-browser web development tool called Firebug with an API for PHP web application developers. FirePHP is free and easily attainable via the Mozilla Add-Ons section on the official Mozilla site. The official FirePHP site can be found via this web address: www.firephp.org.


How to get started with FirePHP? 


To get started with FirePHP we need the following -
1) FireFox
2) FireBug
3) FirePHP
4) FirePHP Core (a PHP class to send the headers)


How to install the FirePHP Core? 
Checkout @ http://www.firephp.org/HQ/Learn.htm


Logging messages and information in Firebug Console. 


Once everything is setup you are ready to debug your PHP Apps. To learn and test the workings of FirePHP simply create a index.php in your root folder and add the following lines -




require_once('FirePHPCore/FirePHP.class.PHP');
$firephp = FirePHP::getInstance(true);
$firephp->fb("Starting ... "); 
$firephp->log("This is a Log Message");


$fruits = array("Apple", "Banana", "Mango", "Orange", "Pear");
$firephp->log($fruits, "Dumping Fruits Array");


$firephp->info("This is an informational message");
$firephp->error("This is an error message");
$firephp->fb("End!");
Run the above index.php and check the firebug console!!!





Move your cursor on the array Dump ... 


A WORD of caution
1) Do not forget to disable FirePHP on LIVE site as it may expose sensitive data of your website to anyone who has FirePHP & FireBug installed. To disable firePHP use - 
  $firephp->setEnabled(false);


Further reading: http://www.christophdorn.com/ 

No comments:

Post a Comment