Advertisement
Guest User

php error handler

a guest
Nov 18th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2.     $old_error_handler = set_error_handler("userErrorHandler");
  3.    
  4.     // we will do our own error handling
  5.     error_reporting(0);
  6.  
  7. // user defined error handling function
  8. function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
  9. {
  10.     // timestamp for the error entry
  11.     $dt = date("Y-m-d H:i:s (T)");
  12.  
  13.     // define an assoc array of error string
  14.     // in reality the only entries we should
  15.     // consider are E_WARNING, E_NOTICE, E_USER_ERROR,
  16.     // E_USER_WARNING and E_USER_NOTICE
  17.     $errortype = array (
  18.                 E_ERROR              => 'Error',
  19.                 E_WARNING            => 'Warning',
  20.                 E_PARSE              => 'Parsing Error',
  21.                 E_NOTICE             => 'Notice',
  22.                 E_CORE_ERROR         => 'Core Error',
  23.                 E_CORE_WARNING       => 'Core Warning',
  24.                 E_COMPILE_ERROR      => 'Compile Error',
  25.                 E_COMPILE_WARNING    => 'Compile Warning',
  26.                 E_USER_ERROR         => 'User Error',
  27.                 E_USER_WARNING       => 'User Warning',
  28.                 E_USER_NOTICE        => 'User Notice',
  29.                 E_STRICT             => 'Runtime Notice',
  30.                 E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
  31.                 );
  32.     // set of errors for which a var trace will be saved
  33.     $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
  34.    
  35.     $errtype = $errortype[$errno];
  36.    
  37.     $err = "<errorentry>\n";
  38.     $err .= "\t<datetime>" . $dt . "</datetime>\n";
  39.     $err .= "\t<errornum>" . $errno . "</errornum>\n";
  40.     $err .= "\t<errortype>" . $errtype . "</errortype>\n";
  41.     $err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
  42.     $err .= "\t<scriptname>" . $filename . "</scriptname>\n";
  43.     $err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";
  44.  
  45.     if (in_array($errno, $user_errors)) {
  46.         $err .= "\t<vartrace>" . wddx_serialize_value($vars, "Variables") . "</vartrace>\n";
  47.     }
  48.     $err .= "</errorentry>\n\n";
  49.    
  50.     $file = $filename;
  51.     $file = str_replace("H:\\xampp\\htdocs\\!webpaket","", $file);
  52.     $err2 = "<b>".$errtype.":</b><br>".$errmsg."<br>in Zeile ".$linenum."<br>".$file."<br>";
  53.    
  54.     if(isLocal())
  55.     {
  56.         $t = file($filename);
  57.        
  58.         $von = $linenum-4;
  59.         if($von<0) $von=0;
  60.         $bis = $linenum+4;
  61.        
  62.         $err2.="<div class=error>";
  63.         for($i = $von; $i <= $bis; $i++)
  64.         {
  65.             if($i==$linenum-1) $err2.="<span>";
  66.             $zeile = $t[$i];
  67.             $zeile = str_replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;", $zeile);
  68.             $err2.= $zeile;
  69.             if($i==$linenum-1)
  70.             {
  71.                 $err2.="</span>";
  72.             }else{
  73.                 $err2.="<br>";
  74.             }
  75.         }
  76.         $err2.="</div>";
  77.     }
  78.     $err2.="<br>";
  79.    
  80.     // for testing
  81.     header("HTTP/1.1 500 Internal Server Error");
  82.     //echo $err;
  83.     echo $err2;
  84.  
  85.     // save to the error log, and e-mail me if there is a critical user error
  86.     //error_log($err, 3, "/usr/local/php4/error.log");
  87.     if ($errno == E_USER_ERROR) {
  88.         //mail("phpdev@example.com", "Critical User Error", $err);
  89.     }
  90. }
  91.    
  92. ?>
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement