如果模式是生产模式,则我不想向用户显示任何致命错误或警告等。我要实现的是检查是否发生任何类型的错误,将数据存储在会话数组中,而不是重定向到某些自定义页面。
问题
重定向到其他页面后,存储所有错误的会话数组显示为空?
模式制作
比执行error_handler函数
if($GLOBAL['MODE'] == 'PROD'){
ini_set('log_errors',FALSE);
set_error_handler("errorHandler");
}
代码
ini_get(0);
//Declare session array
$_SESSION['CUSTOM_ERROR'] = array();
register_shutdown_function("shutdownHandler");
function errorHandler($error_level, $error_message, $error_file,
$error_line, $error_context)
{
$error = " Msg:" . $error_message . " file:" . $error_file . " ln:" .
$error_line;
switch ($error_level) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_PARSE:
$_SESSION['CUSTOM_ERROR'] = $error;
mylog($error, "fatal");
break;
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
$_SESSION['CUSTOM_ERROR'] = $error;
mylog($error, "error");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
//Store all errors in session array <<<<---------
$_SESSION['CUSTOM_ERROR'] = $error;
mylog($error, "warn");
break;
case E_NOTICE:
case E_USER_NOTICE:
$_SESSION['CUSTOM_ERROR'] = $error;
$_SESSION['CUSTOM_ERROR'] = $error;
mylog($error, "info");
break;
case E_STRICT:
$_SESSION['CUSTOM_ERROR'] = $error;
mylog($error, "debug");
break;
default:
$_SESSION['CUSTOM_ERROR'] = $error;
mylog($error, "warn");
}
}
function shutdownHandler() //will be called when php script ends.
{
$lasterror = error_get_last();
switch ($lasterror['type'])
{
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_PARSE:
$_SESSION['CUSTOM_ERROR'] = $error;
$error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" .
$lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" .
$lasterror['line'];
mylog($error, "fatal");
}
}
function mylog($error, $errlvl)
{
if($error){
//redirect to custom-page
}
}
自定义页面
print_r($_SESSION['CUSTOM_ERROR']); //Problem its show empty
die;
为了更好地理解我要简单解决的问题
$_SESSION['CUSTOM_ERROR'] = set_error_handler("errorHandler");
if(!empty($_SESSION['CUSTOM_ERROR'])){
//redirect
}