Clever HTTP

I was looking at FirePHP today, trying to figure out what the point is, because the screenshots they keep directing me to are a joke.

Here is a better summary:

Basically it is allows PHP scripts to send debugging (or profiling) information to Firefox’s Firebug without having the write to the page itself.

How it does this is quite clever.

[More on FirePHP after the jump]

Backhanding yourself the info

The standard way of debugging in PHP involves just echoing shit to the screen. The fact that PHP is a scripting language makes this an excellent way of debugging on the web. But what happens when doing this breaks your site? This is especially relevant when you are debugging a well-formed XML or JSON response.

The trick is to use detect the incoming HTTP request and then if Firebug is enabled, to send a multipart/mixed MIME return. The first part contains the primary data and the second part contains a text/firephp file which consists of an XML of the information you have displayed. Right now all you do is create HTML and it just renders it in the debugging window.

That’s pretty clever.

Using it

The problem is I want to use it on the existing site without changing anything. The way the PEAR library is written, it doesn’t play nice with output buffering or header calls, so I tried my own version. You can download it here.

(Because I’m on PHP 4 right now at work, I made it PHP 4 compatible.) There is some strange bug where it doesn’t always work for me every request. (If you can fix this bug, I’d appreciate it.)

To install, just drop the files in your include_path and make sure you have the Firefox FirePHP extension installed. (Sorry, I don’t care to make this a PEAR package, maybe later.)

Then, you just create a page that looks like this:

[PHP]
require_once(‘firephp/tools.php’);
firephp_tools::start();

// Write something to browser
echo ‘this is a test’;

// Write something to FirePHP
firephp_tools::appendContent(sprintf(‘<p>reached line %s file %s.</p>’,__LINE__,__FILE__));
firephp_tools::dump($GLOBALS,’GLOBALS’);

// ob_end_flush() will get called automagically
[/php]

And visit it with firebug on. (Note: the only relevant part is the first two lines. The rest is so you can understand how the features work.)

FirePHP example

Cake.

Just testing

Adding it to my local configuration in development (optimizations off) yields the following:

page: Tagged homepage
PHP execute time: 0.4645s

page: mypage
PHP execute time: 1.0758s

page: profile page (of someone else)
PHP execute time: 1.9655s
PHP execute time (after reload): 1.2070s

I have a lot of work ahead of me.

12 thoughts on “Clever HTTP

  1. Pingback: PHPDeveloper.org
  2. I read about the project earlier today, but i also thought the firephp page was extremely confusing, and the demo/screenshot page doesnt give you any real value without reaaaally trying…

    I just tried it in our company dev environment, and it doesn’t seem to work properly ..

    I think it has some trouble with javascript includes, they get all squashed up when i load the page…

    I havent had the time to investigate the problem further, but i thought i would let you know, maybe others will experience the problem too ..

    The whole project seem like a really neat idea, i would love to be able to debug php in a “firebug-style” manner, and it would be really nice if it was super easy to use…

    That’s one of the keys to firebug, just install and use – no hassle at all.

    Keep up the good work.. 🙂

  3. Aw, it really is not clear in my post that i meant it was *your* version i’ve been trying out.. not the “official” PEAR package stuff..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.