note 60238 added to ref.errorfunc
| From: | brendy at gmail dot com | Date: | Fri, 30 Dec 2005 23:02:39 +0000 |
| Subject: | note 60238 added to ref.errorfunc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101013@lists.php.net to get a copy of this message | ||
Here is a good general purpose error handling function which will display the GLOBALS, while
protecting most sensitive data from being displayed:
USAGE:
<?PHP
$foo = bar() or die( showError(__LINE__,__FILE__) );
?>
FUNCTION:
<?PHP
function showError( $line , $file ) {
$vars = array(
'CONSTANTS' => get_defined_constants() ,
'SESSION' => $_SESSION ,
'FILES' => $_FILES ,
'COOKIES' => $_COOKIE ,
'POST' => $_POST ,
'GET' => $_GET ,
'SERVER' => $_SERVER
);
$mysqlError = mysql_error();
$mysqlLastID = mysql_insert_id();
$mysqlInfo = mysql_info();
$ERR = "";
$globalsIndex = 0;
$ERR .= "<!-- BEGIN ERROR -->\n";
$ERR .= "<style type='text/css'>\n";
$ERR .= "pre#commerceError { font-size:85%; width:75%; margin:10px; border:1px dashed green;
padding:10px; background-color:#EFFFAD; }\n";
$ERR .= "div.commerceErrorDetails { background-color:#EEE; margin-top:10px;
margin-bottom:10px; border:1px solid #777; border-collapse:collapse; padding:10px; }\n";
$ERR .= "h3.commerceErrorTitle { font-weight:bold; color:red; }\n";
$ERR .= "ul#commerceErrorInfo li { font-weight:bold; color:blue; }\n";
$ERR .= "span.commerceErrorKey { font-weight:bold; }\n";
$ERR .= "</style>\n";
$ERR .= "<pre id='commerceError'>\n";
$ERR .= "<h2>An Error Has Occurred.</h2>\n";
$ERR .= "<ul id='commerceErrorInfo'>\n";
$ERR .= "<li>FILE: {$file}</li>\n";
$ERR .= "<li>LINE: {$line}</li>\n";
$ERR .= "<li>MYSQL ERROR: {$mysqlError}</li>\n";
$ERR .= "<li>MYSQL LAST INSERT: {$mysqlLastID}</li>\n";
$ERR .= "<li>MYSQL INFO: {$mysqlInfo}</li>\n";
$ERR .= "</ul>\n";
$ERR .= "<div class='commerceErrorDetails'>";
$ERR .= "<h3 class='commerceErrorTitle'>GLOBALS</h3>\n";
//globals
foreach( $GLOBALS AS $key => $val) {
//skip non user vars
if(
!(bool)$globalsIndex ||
(bool)preg_match( "/^((http)?_|GLOBALS)/i" , $key ) ||
(bool)array_key_exists( $key , $_SERVER ) ||
(bool)array_key_exists( $key , $_ENV )
) {
$globalsIndex++;
continue;
}
$thisList = print_r( $val , TRUE );
$hiddenText = preg_replace( "/(.*?)(pass|user|session)(.*?)(=>\s*)(.*)/i" ,
"\\1\\2\\3\\4******" , $thisList );
$hiddenText = preg_replace( "/[a-z0-9]{26,32}/i" ,
"********************************" , $hiddenText );
$ERR .= "\n\n";
$ERR .= "<span class='commerceErrorKey'>{$key}</span> =
{$hiddenText}";
$globalsIndex++;
}
$ERR .= "</div>";
//all other vars
foreach( $vars AS $varType => $varList ) {
$ERR .= "<div class='commerceErrorDetails'>";
$ERR .= "<h3 class='commerceErrorTitle'>{$varType}</h3>\n";
$ERR .= print_r( $varList , TRUE );
$ERR .= "</div>";
}
$ERR .= "</pre>";
$ERR .= "<!-- END ERROR -->\n";
return $ERR;
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.errorfunc.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60238
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60238&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60238&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60238&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60238&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60238&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60238&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60238&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60238&report=yes
Search -- http://master.php.net/manage/user-notes.php