Zend - The PHP Company




Utilities

Add Code


Error handler with several output options  

Type: code fragment
Added by: gsnt
Entered: 24/07/2001
Last modified: 07/12/2000
Rating: - (fewer than 3 votes)
Views: 6689
This error handler either outputs error messages together with current variables (type, name, value) and GLOBALS in a separate browser window(for development), writes to a file or sends a message to anyone (webmaster for example).


<?php
// Error handler (Gilbert Saint, gsaint@couf.net) 2001/07/23 v 1.0
//-------------------------------------------------------------
/* ERR_REPORT indicates how error messages should be output :
    ERR_ON_HTML_WIN : displayed in a special browser window
    ERR_ON_FILE : written on a file (name of the file : ERR_FILE)
    ERR_BY_MSG : send a message,
        ERR_MSG_ADDR = To address, ERR_MSG_SUBJ Subject
    any combination of the above is accepted
        
    ERR_SITE_MSG : message displayed on the page where error occurs
    */
define("GS_NL_SEP","'gs'nl'");         // temp separator equivanet to nl
define("GS_TAB_SEP","'gs'tab'");    // temp separator equivalent to tab
define("ERR_ON_FILE",        1);
define("ERR_ON_HTML_WIN",    2);
define("ERR_BY_MSG",        4);

error_reporting(0);

function 
htmlize_gs_serial($err) {
// transforms input string to HTML : 
//    replaces GS_NL_SEP by <br> and GS_TAB_SEP by &nbsp; and spaces
$err =  str_replace(GS_NL_SEP,"<br />",htmlentities($err));
$err str_replace(GS_TAB_SEP,"&nbsp; &nbsp; &nbsp;",$err);
return 
AddSlashes($err);
}

function 
textize_gs_serial($err) {
// transforms input string to text
//    replaces GS_NL_SEP by n and GS_TAB_SEP by t
$err =  str_replace(GS_NL_SEP,"n",$err);
$err str_replace(GS_TAB_SEP,"t",$err);
return 
$err;
}

function 
gs_serialize_array($var,$prelig="") {
// outputs a string including type, name and value of each variable of array $var
//    calls itself recursively when variables are arrays or objects
//    indents output string using a line prefix and temp separators

if (sizeof($var)==0) return "$prelig<empty>".GS_NL_SEP;
$str "";
reset($var);
while (list (
$key$val) = each ($var)) {
    
$gtype gettype($val);
    if (
$gtype == "array") {
        if (
$key != "GLOBALS"
        
$str .= "$prelig$gtype : $key".GS_NL_SEP.gs_serialize_array($val,$prelig.GS_TAB_SEP);
        }
    else if (
$gtype == "object") {$str .= "$prelig$gtype : $key".GS_NL_SEP.gs_serialize_array(get_object_vars($val),$prelig.GS_TAB_SEP);}
    else if (
$gtype =="resource") { $str .= "$prelig$gtype : $key => ".get_resource_type($key).GS_NL_SEP; }
    else if (
$gtype == "string")
        {
$str .=  "$prelig$gtype : $key => ".AddCSlashes($val,"..31").GS_NL_SEP; }
    else 
        {
$str .=  "$prelig$gtype : $key => $val".GS_NL_SEP; }
    }
return 
$str;
}

function 
gs_error_handler($errno$errstr$errfile$errline$vars) {
// error handler for all errors except E_NOTICE
//    outputs an error message containing date, file, line and error message as well as
//    the list of all current variables (type, name, value)
//    the error message is processed according to value of ERR_REPORT

if ($errno == E_NOTICE) return;

$date date("d/m/Y H:i:s");
$err "-----------------------------------------------------".GS_NL_SEP."$date | $errfile, ln : $errline -- err n�$errno$errstr".GS_NL_SEP."Variables :".GS_NL_SEP;
if (
strpos($errstr,'PEAR : ') === false) { $err .= gs_serialize_array($vars);}

// include GLOBALS if they are not part of $vars
reset($vars);
$glob sizeof(array_keys($vars,"PHP_SELF"));
if (
$glob==0$err .= GS_NL_SEP."GLOBALS".GS_NL_SEP.gs_serialize_array($GLOBALS);

if (
ERR_REPORT ERR_ON_HTML_WIN) {
    
$erra htmlize_gs_serial($err);
    echo 
"<SCRIPT type="text/javascript">n";
    echo 
"<!--n";
    echo 
"    errwin = window.open ("","ErrWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=600");n";
    echo 
"errwin.document.write("<html><head><title>Errs</title></head><body>");n";
    echo 
"errwin.document.write("$erra");n";
    echo 
"errwin.document.write("</body></html>");n";
    echo 
"//-->n";
    echo 
"</SCRIPT>n";
    }
if (
ERR_REPORT ERR_ON_FILE) {
    
$erra textize_gs_serial($err);
    
error_log($erra3ERR_FILE);
    }
if (
ERR_REPORT ERR_BY_MSG) {
    
$erra textize_gs_serial($err);
    
mail(ERR_MSG_ADDR,ERR_MSG_SUBJ,$erra);
    }

die(
"<br><br><b>".ERR_SITE_MSG."</b>");
}

set_error_handler('gs_error_handler');

// to process PEAR errors
/*
function pear_error_handler($err_obj) {
$error_string =$err_obj->getMessage()."/".$err_obj->getDebugInfo();
trigger_error("PEAR : ".$error_string, E_USER_ERROR);
}

require_once("PEAR.php");
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,'pear_error_handler');
*/
?>


Usage Example


<?php
require_once("errhandler.php");
define("ERR_REPORT",ERR_ON_HTML_WIN); // outputs on separate browser window
define("ERR_SITE_MSG","Sorry, error on the server...");

echo 
"Checks the error handler...";
$k 1/0;
?>


Rate This Script





Search



This Category All Categories