Welcome Guest, Not a member yet? Register   Sign In
[CODE] Easy On/Off Debugging and Profiling
#1

[eluser]pbarney[/eluser]
If you're like me, you're often putting var_dump or $this->output->enable_profiler() in your source code while you're debugging it.

I got tired of commenting and un-commenting these lines, so I came up with an easier system. Some of you may be familiar with the PHP XDEBUG extension. Whether or not you use it, the Firefox add-on Easy XDebug (any also, I believe, the Chrome add-on Xdebug Helper) provides a really nice way to turn our debugging messages on and off.

For all else they may do, the extensions do one interesting thing in particular: set or delete two special cookies (XDEBUG_SESSION and XDEBUG_PROFILE) that we can use to let our app know if we're in "debugging mode" or "profile mode".

So here's what I came up with:

In config.php, add the following block:
Code:
/*
|--------------------------------------------------------------------------
| XDEBUG Debugger and Profiler setting
|--------------------------------------------------------------------------
|
| If the Firefox Xdebug Helper add-on is installed and enabled, the cookie
| 'XDEBUG_PROFILE' or "XDEBUG_SESSION" can be set. If so, indicate that by
| setting a constant.
|
*/

// which IP's are allowed to debug?
$allowed_debugging_hosts = array(
    '127.0.0.1',    // localhost
    '256.128.64.32' // developer's "command center" in mom's basement
);

// only allow specific IP's the ability to debug
$trusted = (in_array($_SERVER['REMOTE_ADDR'],$allowed_debugging_hosts));

define('PROFILER', isset($_COOKIE['XDEBUG_PROFILE']) && $trusted);
define('DEBUG', isset($_COOKIE['XDEBUG_SESSION']) && $trusted);

Now you can do handy things like:
Code:
if (DEBUG) echo "<pre>" . print_r($this->session->userdata,true) . "</pre>";
// and
if (PROFILER) $this->output->enable_profiler();

Once you've installed the browser add-on, just click either of the tiny green buttons in the status bar to enable or disable either debugging or profiling.

(Note: If you're behind a proxy, you might also want to check $_SERVER['HTTP_X_FORWARDED_FOR'] when checking for the trusted host ip, which I leave as an exercise for the reader.)

Thoughts?




Theme © iAndrew 2016 - Forum software by © MyBB