progman1010
Programmer
Does anyone know of a quick and easy way to set_error_hander and have it redirect to a 'sorry, we've encountered an error...' page? I'm having trouble actually getting the redirect to work.
here's my code so far...
here's my code so far...
Code:
//my standard redirect function...
function redirect($url,$clear=false) {
if (!$clear) { //if clear is true, then it will skip the session vars and all.
//check for session first
if ($_SESSION['redirect']!='') $url=$_SESSION['redirect'];
//url request supercedes session var
//use this for special overrides- allows for universal return pages
if ($_REQUEST['redirect']!='') $url=$_REQUEST['redirect'];
}
//replace special chars so that you get less errors
$url=str_replace('~','&',$url);
unset($_SESSION['redirect']); // clear
if (substr($url,0,1)!="?") $url="?".$url;
if (!headers_sent()) header("Location: index.php$url");
else echo "<script>window.location=\"index.php$url\";</script><noscript>Javascript redirect didn't work.<br /><a href=\"$url\">Click here to go to this page.</a></noscript>";
}
//now, the error handler
error_reporting(0);
// 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'
);
// user defined error handling function
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
{
global $errortype;
// timestamp for the error entry
$dt = date("Y-m-d H:i:s (T)");
// set of errors for which a var trace will be saved
$user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
$err = "<errorentry>\n";
$err .= "\t<datetime>" . $dt . "</datetime>\n";
$err .= "\t<errornum>" . $errno . "</errornum>\n";
$err .= "\t<errortype>" . $errortype[$errno] . "</errortype>\n";
$err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
$err .= "\t<scriptname>" . $filename . "</scriptname>\n";
$err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";
if (in_array($errno, $user_errors)) {
$err .= "\t<vartrace>" . wddx_serialize_value($vars, "Variables") . "</vartrace>\n";
}
$err .= "</errorentry>\n\n";
if (!in_array($errno, array(E_NOTICE,E_USER_NOTICE))) {
// save to the error log, and e-mail me if there is a critical user error
error_log($err, 3, $doc_root."error.log");
//mail(ERROR_NOTIFICATION_EMAIL, "Critical User Error", $err);
redirect("/index.php?content=error");
}
}
set_error_handler("userErrorHandler");