Intercepta los errores de ejecución y los envía por mail al administrador con detalle del contexto


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function error_handler($errno, $errstr, $errfile, $errline, $errctx) {
  2.  
  3. $EN_DESARROLLO = (substr_count($_SERVER['HTTP_HOST'],"localhost")==0)? false : true;
  4.  
  5. $host = $_SERVER['HTTP_HOST'];
  6. $mail_subject = 'Error at '.$host;
  7. $mail_from = '[email protected]';
  8.  
  9. $errortype = array(1=>"Error", 2=>"Warning", 4=>"Parsing Error", 8=>"Notice", 16=>"Core Error", 32=>"Core Warning", 64=>"Compile Error", 128=>"Compile Warning", 256=>"User Error", 512=>"User Warning", 1024=>"User Notice", 2048=>"PHP5 Strict Notice");
  10.  
  11. $error_handler_string = "<font size=2 face=Arial><h3>Error at ".$host."<br></h3><b>Date: </b>".date('F j, Y, H:i:s a')."<br><b>Error Type: </b>". $errortype[$errno]." (".$errno.")<br><b>Description: <font color=ff0000>".$errstr."</font></b><br><b>Error File: </b>".$errfile."<br><b>Error Line: </b>".$errline."<br><br>";
  12.  
  13. while( isset($_SESSION) && list($var, $val) = each($_SESSION) ) $error_handler_string .= "_SESSION[".$var."] = ".$val."<BR>";
  14. while( isset($_GET) && list($var, $val) = each($_GET) ) $error_handler_string .= "_GET[".$var."] = ".$val."<BR>";
  15. while( isset($_POST) && list($var, $val) = each($_POST) ) $error_handler_string .= "_POST[".$var."] = ".$val."<BR>";
  16. while( isset($_COOKIE) && list($var, $val) = each($_COOKIE) ) $error_handler_string .= "_COOKIE[".$var."] = ".$val."<BR>";
  17.  
  18. if( $EN_DESARROLLO ){
  19. die($error_handler_string);
  20. }else{
  21. @ini_set("sendmail_from",$mail_from);
  22. foreach( $mail_to as $mail_to_str ){
  23. mail($mail_to_str, $mail_subject, $error_handler_string, "From: ".$mail_from."\r\nContent-Type: text/html; charset=\"iso-8859-1\"\r\n");
  24. }
  25. if ($errno & (E_WARNING | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR)){
  26. header("Location: http://".$host);
  27. exit();
  28. }
  29. }
  30. }
  31. set_error_handler("error_handler");
  32. ini_set('display_errors','on');

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.