note 60238 deleted from ref.errorfunc by nlopess

From: Date: Fri, 30 Dec 2005 23:04:58 +0000
Subject: note 60238 deleted from ref.errorfunc by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-101016@lists.php.net to get a copy of this message
Note Submitter: brendy at gmail dot com ---- 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; } ?>

« previous php.notes (#101016) next »