Here is a function which generates a debug report for display or email
using get_defined_vars. Great for getting a detailed snapshot without
relying on user input.
<?php
function generateDebugReport($method,$defined_vars,$email="undefined"){
$ignorelist=array("HTTP_POST_VARS","HTTP_GET_VARS",
"HTTP_COOKIE_VARS","HTTP_SERVER_VARS",
"HTTP_ENV_VARS","HTTP_SESSION_VARS",
"_ENV","PHPSESSID","SESS_DBUSER",
"SESS_DBPASS","HTTP_COOKIE");
$timestamp=date("m/d/y h:m:s");
$message="Debug report created $timestamp\n";
global $link;
$sql_error=mysql_error($link);
if($sql_error){
$message.="\nMysql Messages:\n".mysql_error($link);
}
foreach($defined_vars as $key=>$val){
if(is_array($val) && !in_array($key,$ignorelist) && count($val) > 0){
$message.="\n$key array (key=value):\n";
foreach($val as $subkey=>$subval){
if(!in_array($subkey,$ignorelist) && !is_array($subval)){
$message.=$subkey." = ".$subval."\n";
}
elseif(!in_array($subkey,$ignorelist) && is_array($subval)){
foreach($subval as $subsubkey=>$subsubval){
if(!in_array($subsubkey,$ignorelist)){
$message.=$subsubkey." = ".$subsubval."\n";
}
}
}
}
}
elseif(!is_array($val) && !in_array($key,$ignorelist) && $val){
$message.="\nVariable ".$key." = ".$val."\n";
}
}
if($method=="browser"){
echo nl2br($message);
}
elseif($method=="email"){
if($email=="undefined"){
$email=$_SERVER["SERVER_ADMIN"];
}
$mresult=mail($email,"Debug Report for ".$_ENV["HOSTNAME"]."",$message);
if($mresult==1){
echo "Debug Report sent successfully.\n";
}
else{
echo "Failed to send Debug Report.\n";
}
}
}
?>