自定义PHP错误
PHP代码
- // we will do our own error handling
- error_reporting(E_ALL);
- // user defined error handling function
- function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
- {
- // timestamp for the error entry
- $dt = date("Y-m-d H:i:s (T)");
- // define an assoc array of error string
- // in reality the only entries we should
- // consider are E_WARNING, E_NOTICE, E_USER_ERROR,
- // E_USER_WARNING and E_USER_NOTICE
- $errortype = array (
- E_ERROR => ‘Error’,
- E_WARNING => ‘Warning’,
- E_PARSE => ‘Parsing Error’,
- E_NOTICE => ‘Notice’,
- E_CORE_ERROR => ‘Core Error’,
- E_CORE_WARNING => ‘Core Warning’,
- E_COMPILE_ERROR => ‘Compile Error’,
- E_COMPILE_WARNING => ‘Compile Warning’,
- E_USER_ERROR => ‘User Error’,
- E_USER_WARNING => ‘User Warning’,
- E_USER_NOTICE => ‘User Notice’,
- E_STRICT => ‘Runtime Notice’,
- E_RECOVERABLE_ERROR => ‘Catchable Fatal Error’
- );
- // set of errors for which a var trace will be saved
- $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
-
$err = "
\n" ; -
$err .= "\t
" . $dt . "\n"; -
$err .= "\t
" . $errno . "\n"; -
$err .= "\t
" . $errortype[$errno] . "\n"; -
$err .= "\t
" . $errmsg . "\n"; -
$err .= "\t
" . $filename . "\n"; -
$err .= "\t
" . $linenum . "\n"; - if (in_array($errno, $user_errors)) {
-
$err .= "\t
" . wddx_serialize_value($vars, "Variables") . "\n"; - }
- $err .= "\n\n";
- // for testing
- // echo $err;
- // save to the error log, and e-mail me if there is a critical user error
- error_log($err, 3, "error.log");
- if ($errno == E_USER_ERROR) {
- mail("[email protected]", "Critical User Error", $err);
- }
- }
- $old_error_handler = set_error_handler("userErrorHandler");
- mysql_connect("localhost", "root", "");
- ?> </li> </ol> </div> 把PHP的错误显示控制住,并按自己的方式显示。