A simple way to debug a php application in production mode

Useful PHP debug functions

A simple way to debug a php application in production mode

Here are a few useful PHP debug functions.

I wrote a similar post sometime ago posting the abbreviated “pr” function as a shorthand replacement for the “prinr_r” or “var_dump” functions. Since then this function has changed a bit so re-posting it here with the improvements. With the new updated function one can restrict the output to screen for specific IP address only.

Lets start by defining the IP address at the top for which you want to display the debug output. If you do not care about displaying output to browser for a specific IP (in case you are on development mode) just comment the following two lines out.

if(!defined('DEBUG_REMOTE_ADDR')) {
 define('DEBUG_REMOTE_ADDR', '77.78.103.12');
}

Next one is a shorthand for die() function. You can restrict a die based on the IP address or just die your script with line number.

if(!function_exists('di')) {
 function di($dt=null) {
 if(defined('DEBUG_REMOTE_ADDR') && $_SERVER['REMOTE_ADDR'] != DEBUG_REMOTE_ADDR) return;
 $bt = debug_backtrace();
 $caller = array_shift($bt);
 $file_line = "<strong>" . $caller['file'] . "(line " . $caller['line'] . ")</strong>\n";
 pr($file_line);
 die($dt);
 }
}

You can output the javascript source code with the following function. By default it does not generate the “<script>” tag. If you want to generate the “<script>” tag just pas the second argument as true. For example, <code>djs(‘alert(“Hi”)’, true)</code>

if(!function_exists('djs')) {
 function djs($dt=null, $outTag = false) {
 if(defined('DEBUG_REMOTE_ADDR') && $_SERVER['REMOTE_ADDR'] != DEBUG_REMOTE_ADDR) return;
 $bt = debug_backtrace();
 $caller = array_shift($bt);
 $file_line = $caller['file'] . "(line " . $caller['line'] . ")";
 if($outTag) print('<script>
 ');
 print('//Debugger Line: ' . $file_line . '
 ' . $dt);
 if($outTag) print('</script>');
 }
}

Finally, below is the debug function named “pr”. This useful php debug function is a shorthand mix of the popular print_r and var_dump functions. It can be used to print output on a live website while using a check for ip address or the debug output can be emailed.

It works similar to one posted previously except a few changes which includes the inclusion of IP check and line number in the HTML source code

if(!function_exists('pr')) {
 function pr($p, $func="print_r",$r=false) {
 if(defined('DEBUG_REMOTE_ADDR') && $_SERVER['REMOTE_ADDR'] != DEBUG_REMOTE_ADDR) return;
 if(!function_exists($func)) {
 die("Debug function {$func} does not exist!");
 }
 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 print
 echo '<pre>';
 echo '<!--Debugger Line: ' . $file_line . '-->' . $dt;
 print_r($file_line);
 $func($p);
 echo '</pre>';
 } else { //if return
 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;
 }
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *