自定义错误处理程序不处理严重错误(PHP)

时间:2010-10-21 22:13:11

标签: php error-handling

这是我的错误处理程序的主要部分:

function my_error_handler($errno, $errstr, $errfile, $errline){
    $errno = $errno & error_reporting();
    if($errno == 0) return;
    if(!defined('E_STRICT'))            define('E_STRICT', 2048);
    if(!defined('E_RECOVERABLE_ERROR')) define('E_RECOVERABLE_ERROR', 4096);

    $error = ""; 

    switch($errno){
        case E_ERROR:               $error = $error."Error";                  break;
        case E_WARNING:             $error = $error."Warning";                break;
        case E_PARSE:               $error = $error. "Parse Error";            break;
        case E_NOTICE:              $error = $error. "Notice";                 break;
        case E_CORE_ERROR:          $error = $error. "Core Error";             break;
        case E_CORE_WARNING:        $error = $error. "Core Warning";           break;
        case E_COMPILE_ERROR:       $error = $error. "Compile Error";          break;
        case E_COMPILE_WARNING:     $error = $error. "Compile Warning";        break;
        case E_USER_ERROR:          $error = $error. "User Error";             break;
        case E_USER_WARNING:        $error = $error. "User Warning";           break;
        case E_USER_NOTICE:         $error = $error. "User Notice";            break;
        case E_STRICT:              $error = $error. "Strict Notice";          break;
        case E_RECOVERABLE_ERROR:   $error = $error. "Recoverable Error";      break;
        default:                    $error = $error. "Unknown error ($errno)"; break;
    }
    $error = " <i>$errstr</i> in <b>$errfile</b> on line <b>$errline</b><br /> \n";
    $gi = geoip_open("GeoIP/GeoLiteCity.dat",GEOIP_STANDARD);

    $ip = ip(); 

    $user = $_SESSION["username"];  

    $country = geoip_country_name_by_name($gi, $ip);    

    $error .= "User: <b>".$user."</b> Country: <b>".$country."</b> Ip: <b>".$ip."</b> <br /><br /> \n"; 

    //Bruk ip function og kanskje geoip isteden?

    echo "<p>Something went wrong. It has been logged and will be fixed shortly. Thanks </p>";  
    //echo $error; 

    log_error_ereg($error);

    }

这通常工作正常,但是如果我得到一个严重的错误,它会显示它显示php文件名,出了什么问题等等。我要隐藏用户的东西。如何在错误处理程序中包含此类错误?似乎我的错误处理程序没有选择了..

2 个答案:

答案 0 :(得分:2)

来自set_error_handler的PHP手册:

使用用户定义的函数处理以下错误类型 :E_ERROR,E_PARSE,E_CORE_ERROR,E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,以及调用set_error_handler()的文件中引发的大部分E_STRICT。

答案 1 :(得分:1)

你的错误处理程序需要返回true - 如果它返回false,那么php默认错误处理程序将在你完成之后继续。

另外,将display_errors设置为false:

ini_set('display_errors', false);