# or: # - in this smart & short case DEBUG will set to 1 ## The constant DEBUG is also used in this ways: # DEBUG==0 or smaller than 0: # --->The script will NOT break on notices or warnings but show them the notices or warnings, not the source! # DEBUG==1 or greater than 2: # --->The script will hard break on ALL notices and warnings, but not the source of the errors... (standard) # DEBUG==2; # --->The script will show the errorous source & break on ALL notices and warnings... ## Editors - If you use Windows and have Errors # Do not safe this with a BOM! Maybe your Skripts can not work fine if you do this. # Show the fine manual of your text-editor. ### All stuff written. Let's go!: */ error_reporting(E_ALL); ini_set("html_errors", 1); set_error_handler( function($errNo, $errStr, $errFile, $errLine) { if ( ! defined('DEBUG') ) { define('DEBUG', 1); } if (! headers_sent()) { header_remove(); header("Cache-Control: no-store, no-cache, must-revalidate"); header($_SERVER['SERVER_PROTOCOL'].'/1.1 500'); header("Status: 500"); header('Content-Type: text/html; charset=utf-8'); } $msg = "$errStr in $errFile on line $errLine"; if ($errNo == E_NOTICE) { $errType="notice"; } elseif ($errNo == E_WARNING) { $errType="warning"; } elseif ($errNo == E_USER_WARNING) { $errType="self triggered warning"; } elseif ($errNo == E_USER_NOTICE) { $errType="self triggered notice"; } elseif ($errNo == E_USER_ERROR) { $errType="self triggered error"; } else { $errType="unknown"; } if ( $errType ) { if ( 2==DEBUG ) { $arErrFile=file($errFile); $errMin=$errLine-4; if ($errMin < 1) { $errMin=1; } $errMax=$errLine-1; if ($errMax >= count($arErrFile)) { $errMax=count($arErrFile)-1; } for ($i=$errMin; $i<=$errMax; $i++) { if (( $i > 1) && (isset($arErrFile[$i])) ) { if ( $i == $errLine-1) { $arErrText[]="$i:\t" . htmlspecialchars(rtrim($arErrFile[$i])). "\n"; } else { $arErrText[]="$i:\t" . htmlspecialchars($arErrFile[$i]) . ""; } } } unset($arErrFile); } else { $arErrText[0]="If you wish to see the relavant part of th source here, then define DEBUG und set this to 2."; } $my_php_error_msg_id='phperr_'.rand(); $errStr = preg_replace("/ \[.*<\/a>\]/", "", $errStr); if ( 0 < DEBUG ) { $errBreaked="The execution of your fine script was hard breaked. Why?"; $errScript=''; } else { $errBreaked=' --- '; $errScript=" "; } #The first line of the output is helpfuel if the calling script works in a special context. Naturally is this not valid html... print "

Error! (Type: "$errType")

  • in file:
    " . htmlspecialchars($errFile) . "
  • in row: " . ($errLine-1) . "
  • text: " . htmlspecialchars($errStr) . "
  • the most relevant part of code:
    " . implode('', $arErrText). "

$errBreaked Often the results are not this what expected!

$errScript "; if ( 0 < DEBUG ) { exit; } } else { echo $msg; } }, E_ALL); # vim: set fileencoding=utf-8