set_error_handler, How do I suppress E_NOTICE in php

735 views Asked by At

How do I suppress E_NOTICE in this php function, I don't want to get email, about non declaret variables, from a production site ?

I have tried to out comment case E_NOTICE: but then the program just jumps down to default: exit("Unknown error at $errfile:$errline"); near the end of the script.

I want the opportunity to commented out the error feedback I don't want ??

<?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");
?>
3

There are 3 answers

0
Lan On BEST ANSWER

put this at the top of your page:

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'); 

the set_error_handler tells it what function to use when there's an errro. that function will only log serious errors (not notices) to a file called errors.php

0
xzag On

add break statement after case E_NOTICE

case E_NOTICE:
    break;
case E_USER_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_STRICT:
    ...
0
Manu On
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");
    }
}