How to debug a live website built in php?
Here’s a basic, simple and custom method i usually use to debug live or production web applications (web sites).
First of all i place the following lines of code in a config.php or somewhere so it is loaded in all targeted pages of the website.
[php]$debugMode = $_SERR[‘VEREMOTE_ADDR’]=="117.197.126.23" ? true : false;
if($debugMode) {
error_reporting(E_ALL);
define(‘DEBUG_MODE’, true);
} else {
error_reporting(0);
define(‘DEBUG_MODE’, false);
}[/php]
In general, while in debug mode i would like to show everything. All notices, warnings, deprecated stuff and all. You can play with this area by turning on/off the different filters like E_ALL & ~E_NOTICE and E_ALL & ~E_NOTICE & ~E_DEPRECATED in any of if/else scope above. (To discuss the error_reporting constants further is out of scope for this post.)
Next, i will create two functions and place them in a common file like common.php or functions.php which should be included site-wide as well. Out of the two, the first function is debug() which is called when you want to output anything. Here’s it is.
[php] function debug($d) {
if(DEBUG_MODE) pr($d);
}[/php]
This debug() function checks for DEBUG_MODE and outputs the passed data using a custom pr() function which i will create as under:
[php]function pr($d) {
echo ‘<pre>’;
print_r($d);
echo ‘</pre>’;
}[/php]
Or rather with more advanced debug code with provides an option to return debug value which you can send to your email address as well. It also gives the line number where you output debug data.
[php]function pr($p, $func="print_r",$r=false) {
if(!$func) $func=’print_r’;
$bt = debug_backtrace();
$caller = array_shift($bt);
$file_line = "<strong>" . $caller[‘file’] . "(line " . $caller[‘line’] . ")</strong>\n";
if(!$r) { //if return is false
echo ‘<pre>’;
print_r($file_line);
$func($p);
echo ‘</pre>’;
} else { //if return is true
ob_start();
echo ‘<pre>’;
print_r($file_line);
$func($p);
echo ‘</pre>’;
$d = ob_get_contents();
ob_end_clean();
if(filter_var($r, FILTER_VALIDATE_EMAIL)) {
$headers = ‘From: webmaster@example.com’ . "\r\n" .
‘Reply-To: webmaster@example.com’ . "\r\n" .
‘X-Mailer: PHP/’ . phpversion();
mail($r, ‘Debug Output’, $d, $headers);
}
return $d;
}
}[/php]
The pr() function outputs the passed data i.e. $d in a well formatted way by wrapping the data within <pre> tags. The idea of naming this function comes from CakePHP framework of course, but since the beginning, it had been one of my most used functions while developing in PHP).
Ready to start debugging a live application?
So, let’s say you wanted to debug a website in production mode. I assume you placed two functions debug() and pr() in a file included site-wide and placed the inline php code in a file included at the top of the site pages included site-wide as well. Did you notice $_SERVER[‘REMOTE_ADDR’]==”117.197.126.23″ in the first line of inline code? This is the part where we make change to show the debug output only to ourselves. We just need to replace string 117.197.126.23 with the appropriate value of your remote server (IP address of the server you are surfing through) and call the debug() function wherever needed. Because of the check for IP Address in effect the debug output (which may include notices, warnings based on the configuration you set for error_reporting()) will be visible only to you and rest of the site users will see nothing unusual.
Tip: You can know your IP address by visiting the website http://www.whatismyip.com/