-2

如何在这个 php 函数中抑制 E_NOTICE,我不想从生产站点收到关于非声明变量的电子邮件?

我试图注释 case E_NOTICE: 但是程序只是跳到默认值:exit("Unknown error at $errfile:$errline"); 接近脚本的结尾。

我希望有机会注释掉我不想要的错误反馈??

<?php

function errorHandler($errno, $errstr, $errfile, $errline) {

    function errorMail($errno, $errstr, $errfile, $errline) {

        $recvmail = "yourmail";

    $fejlkode = array(
        1 => "E_ERROR",
        2 => "E_WARNING",
        4 => "E_PARSE",
        8 => "E_NOTICE",
        16 => "E_CORE_ERROR",
        32 => "E_CORE_WARNING",
        64 => "E_COMPILE_ERROR",
        128 => "E_COMPILE_WARNING",
        256 => "E_USER_ERROR",
        512 => "E_USER_WARNING",
        1024 => "E_USER_NOTICE",
        2048 => "E_STRICT",
        4096 => "E_RECOVERABLE_ERROR",
        8192 => "E_DEPRECATED",
        16384 => "E_USER_DEPRECATED",

    );

    $mail = new PHPMailer;
        $mail->From = 'mailSender';
        $mail->FromName = 'your firm name';
        $mail->AddAddress($recvmail, "your name");  // Add a recipient
        $mail->SetLanguage('dk', './');
        $mail->Subject = utf8_decode("Fejl type:" . $errno .' -> '. $fejlkode[$errno]);
        $mail->Body    = utf8_decode($errstr.' <br>'.$errfile.': '.$errline);
        $mail->AltBody = $errstr.$errfile.$errline.$errno;

        if(!$mail->Send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
          exit;
       }  
}

switch ($errno) {
    case E_NOTICE:
    case E_USER_NOTICE:
    case E_DEPRECATED:
    case E_USER_DEPRECATED:
    case E_STRICT:

        errorMail($errno, $errstr, $errfile, $errline);

        break;

    case E_WARNING:
    case E_USER_WARNING:
       errorMail($errno, $errstr, $errfile, $errline);
        break;

    case E_ERROR:
    case E_USER_ERROR:
       errorMail($errno, $errstr, $errfile, $errline);
        exit("FATAL error $errstr at $errfile:$errline");

    default:

        exit("Unknown error at $errfile:$errline");
    }
}

set_error_handler("errorHandler");
?>
4

3 回答 3

2

将其放在页面顶部:

function serious_log_errors($number, $message, $file, $line, $vars)  
{  
if ( ($number !== E_NOTICE) && ($number < 2048) ) 
 { 
$forlogfile = "A serious error ($number) occurred on line $line and in the file: $file.   $message.\r\n";  
error_log($forlogfile, 3, 'errors.php'); 
 }  
}

set_error_handler('serious_log_errors'); 

set_error_handler 告诉它在出现错误时使用什么函数。该函数只会将严重错误(而不是通知)记录到名为 errors.php 的文件中

于 2014-11-11T00:10:46.507 回答
1

在后面添加break语句case E_NOTICE

case E_NOTICE:
    break;
case E_USER_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_STRICT:
    ...
于 2014-02-14T09:12:04.603 回答
1
switch ($errno) {
    case E_NOTICE:
        break; // Do nothing

    case E_USER_NOTICE:
    case E_DEPRECATED:
    case E_USER_DEPRECATED:
    case E_STRICT:

        errorMail($errno, $errstr, $errfile, $errline);

        break;

    case E_WARNING:
    case E_USER_WARNING:
       errorMail($errno, $errstr, $errfile, $errline);
        break;

    case E_ERROR:
    case E_USER_ERROR:
       errorMail($errno, $errstr, $errfile, $errline);
        exit("FATAL error $errstr at $errfile:$errline");

    default:

        exit("Unknown error at $errfile:$errline");
    }
}
于 2014-02-14T09:12:26.020 回答