Debugging PHP-AJAX Applications with FirePHP


While developing DiLoc|Rail I often found it tedious to debug server-side problems in my XmlHttpRequests. When PHP spits out an error, the JSON will become invalid. Allthough I handle this globally and a nice error message is displayed, it is not that optimal. Furthermore if you want to output some debug-info from the server-side processing, you always render your JSON output invalid, making testing even harder, because the client-side code will not be executed as expected because of the JSON errors.

Luckily, my mind reminded me of something I stumbled upon some months earlier. It was labeled “Try this out when you have time” in my mind. – Stupid me, only wanting to try this out if I have time! So I decided the time is right to try out FirePHP now. – And boy is this useful and easy to use! After some weeks of using, I can not imagine working without FirePHP anymore. So let me show you what this is all about.

FirePHP lets you debug your server-side PHP-code right inside Firebug! This is a very smart idea, and it works great. For this to work, you need two things: First, the FirePHP Firefox extension which plugs into Firebug and extends it. Second: the small PHP library to use on the server-side to output messages right into Firebug, without interfering with your normal XML or JSON output.
FirePHP does this by sending the data inside the response-headers of the response. This way, they do not modify the data that you intend to send with your request. The FirePHP Firefox extension then takes care of parsing the additional response-headers and output them inside Firebug. Neat!

Here are some screenshots showing you how this works:


Besides the things I showed in the screenshots you may also output arrays and messages with different styles (warning, error, info) etc.
If you use the current development-version of the firefox plugin (version 0.1.0.1) and a current svn-version of the php library, you can even output stacktraces into firebug:

For more examples head over to the FirePHP webseite which offers a very good documentation on how to use FirePHP. The developer even provides libraries to integrate FirePHP with the Zend Framwork, Symfony, and Aptanas Jaxer. I think it wont be long until someone writes some serverside code for Ruby or .Net which would make it possible to use FirePHP with other languages.

So in essence, what I wanted to say is: Try out FirePHP, its a really nice addition to Firebug, easy to use, and makes it a pleasure to debug server-side code!

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!