Knightnet Site Design - Debug routines

You are in section: Home > Site Design > Template Files

This is an include file for some useful debug type routines.

The main function I use from here is "show_r".

<?php // Functions for debugging (show raw vars, print errors, etc.) // ------------------------------------------------------------------------------------------------- // Screen printing functions used for printing error messages and pre formated messages // ------------------------------------------------------------------------------------------------- // Print an HTML preformatted error message function error($strErr) { echo "\n<div class='phpErr'>",$strErr,"</div>\n"; } // Pretty print a text array - Direct Output ---------------------------------------------------- function prArray($array,$title='') { $row1='<tr><td>'; $row2='</td></tr>'; print('<table>'); if ($title) print "$row1<b>$title</b>$row2"; foreach ($array as $a) print "$row1 $a $row2"; print('</table>'); } # -------------------------------------------------------------------------------------------- // Do a print_r on a web page (pre formatted) -------------------------------------------------- // BUT also handle additional data types function show_r($array,$link='') { if (is_resource($array)) { if (get_resource_type($array) == 'mysql result') { $tbl=mysql_field_table($array,0); $nCols=mysql_num_fields($array); $nRows=mysql_num_rows($array); print("<hr><h6>mySQL Result Resource - Table Name: $tbl</h6>"); print("\nResult contains $nCols columns and $nRows rows<br>"); print("\n<table border=1 style='font-size:x-small'>\n"); print("\t<thead><tr>"); for ($i=0; $i<$nCols-1; $i++) { $fld=mysql_field_name($array,$i); printf("<th>%s</th>",strtoupper($fld)); } print("\n\t</tr></thead>\n"); print("\t<tbody>\n"); $j=1; while ($row = mysql_fetch_row($array)) { print("\t\t<tr>"); for ($i=0; $i<$nCols-1; $i++) { $meta=mysql_fetch_field($array,$i); if ($meta->type=='date') printf("<td style='text-align:center;>%s</td>",@strftime('%d/%b/%y',@strtotime($row[$i]))); else print "<td>$row[$i]</td>"; } print("</tr>\n"); $j++; } @mysql_data_seek($array,0); // reset the result array to the begining print("\t</tbody>\n</table>\n"); // Get the comment for the table if there is one & if a link parameter passed if ($link) { $result=mysql_query("SHOW TABLE STATUS LIKE '$tbl'", $link); $row = mysql_fetch_row($result); $tblComment=$row[14]; $tblLastUpd=$row[11]; print '<table border=1 style="font-size:x-small"><tr>'; print "<td>$tblComment</td>"; print '<tr>'; printf("<td>Data Last Updated: %s</td>",@strftime('%d/%b/%y %H:%M',@strtotime($tblLastUpd))); print '</tr></table>\n'; } print('<hr>'); } else {print '<pre>RESOURCE: '; print get_resource_type($array); print '</pre>';} } else {print '<pre>'; print_r($array); print '</pre>';} } // --------------------------------------------------------------------------------------- // ------ Capture direct output and return to a string ------ function spr_show_r($array) { ob_start(); show_r($array); $a = ob_get_contents(); ob_end_clean(); return $a; } // -------------------------------------------------------- // Show all of the POST & GET parameters function show_passed() { if (empty($_POST)) error("No Post"); else show_r($_POST); if (empty($_GET)) error("No Get"); else show_r($_GET); } // --------------------------------------- function showvar($var, $string = '$var') { // For: TRUE, FALSE, NULL and empty arrays: Print the value and quit the function if($var) {print htmlentities($string . ' = TRUE;') . "\n"; return;} if(!$var) {print htmlentities($string . ' = FALSE;') . "\n"; return;} if($var === null) {print htmlentities($string . ' = NULL;') . "\n"; return;} if($var === array()) {print htmlentities($string . ' = Array();') . "\n"; return;} // Variable is no Object and no array? -> Print the value and quit the funciton. if(!is_object($var) && !is_array($var)) { // Format as a string if it is one if(is_string($var)) $var = "'" . str_replace('\"', '"', addslashes($var)) . "'"; print htmlentities($string . ' = ' . $var) . ";\n"; return; } // Loop for every element of $var foreach($var as $k=>$v) { // Format the string which stands nex to the ' = '. // [] for arrays and -> for objects if(is_array($var)) { if(is_string($k)) $k = "'" . str_replace('\"', '"', addslashes($k)) . "'"; $new_string = $string . '[' . $k . ']'; } else if(is_object($var)) $new_string = $string . '->' . $k; // show the value showvar($v, $new_string); } } // // Debugging and Information // function phpfuncs() { //BY A. Markovic (mikikg@eunet.yu) ECHO "<b><i>Defined Functions for '{$_SERVER['HTTP_HOST']}''</b></i><br>"; ECHO "<br><b>get_loaded_extensions:</b><br>"; $arr=get_loaded_extensions(); show_r(get_loaded_extensions()); ECHO "<br><br><b>get_current_user:</b><br>"; show_r(get_current_user()); ECHO "<br><br><b>getmypid:</b><br>"; show_r(getmypid()); ECHO "<br><br><b>get_extension_funcs() :</b><br>"; for ($i=0;$i<count($arr);$i++) { print "<br><b>$arr[$i]</b><br>"; show_r(get_extension_funcs ($arr[$i])); print "<br>"; flush(); } ECHO "<br><br><b>get_defined_functions() :</b><br>"; show_r(get_defined_functions()); } // Print out the PHP.INI settings (or their overrides) for session handling function print_iniSession() { $x = array( 'session.save_path'=>ini_get('session.save_path'), 'session.name'=>ini_get('session.name'), 'session.save_handler'=>ini_get('session.save_handler'), 'session.auto_start'=>ini_get('session.auto_start'), 'session.gc_probability'=>ini_get('session.gc_probability'), 'session.gc_maxlifetime'=>ini_get('session.gc_maxlifetime'), 'session.serialize_handler'=>ini_get('session.serialize_handler'), 'session.cookie_lifetime'=>ini_get('session.cookie_lifetime'), 'session.cookie_path'=>ini_get('session.cookie_path'), 'session.cookie_domain'=>ini_get('session.cookie_domain'), 'session.cookie_secure'=>ini_get('session.cookie_secure'), 'session.use_cookies'=>ini_get('session.use_cookies'), 'session.use_only_cookies'=>ini_get('session.use_only_cookies'), 'session.referer_check'=>ini_get('session.referer_check'), 'session.entropy_file'=>ini_get('session.entropy_file'), 'session.entropy_length'=>ini_get('session.entropy_length'), 'session.cache_limiter'=>ini_get('session.cache_limiter'), 'session.cache_expire'=>ini_get('session.cache_expire'), 'session.use_trans_sid'=>ini_get('session.use_trans_sid'), 'session.encode_sources'=>ini_get('session.encode_sources'), ); show_r($x); } ?>

Pages:

Valid HTML 4.01 iconValid CSS icon
© Copyright Julian Knight, July 2008 All rights reserved.
Page: Updated 2008-07-10 08:50:07, Author Julian Knight