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.

About tychay

light writing, word loving, ❤ coding
This entry was posted in PHP, web development. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.