Is it possible to manipulate PHP errors via code?

Asked

Viewed 1,122 times

15

As you all know, PHP returns runtime syntax errors in the browser, such as fatal errors and exceptions.

It is possible to handle these errors via code so that I can save to the database in a table of error logs?

The need for this is because I use a PHP file to receive external POST and if I miss the syntax or launch some Exception in this file, I won’t know how and where it occurred.

3 answers

11

Note that depending on the error, the execution of the script will be stopped. If at any time DB gave stopped and generated an error, you will not be able to generate the LOG, so usually the Logs are recorded in a simple TXT, with date-time, file, line, code and message.

Basically you need to use these 3 functions below. They capture all kinds of errors and trigger a Exception which will be where you receive the error information and generates the LOG.


(register_shutdown_function)

Records a function to be executed at the end of the execution. Its function myLog when it is executed you can check the errors using error_get_last()

function myLog()
{
    if( ! is_null( $error = error_get_last() ) )
    {
        // obtendo informações do erro e disparando uma Exception
        extract( $error , EXTR_SKIP );
        new Exception( $message , $type , 0 , $file , $line );
    }
}

register_shutdown_function( 'myLog' );

(set_exception_handler)

Sets a user function for handling exceptions. Usually when a Exception is fired without capture. Its variable $exception is an object and contains the properties of the class with the errors, can take a look at the DOC Exception

function myException( $exception )
{
    // mensagem de erro, grave um TXT-LOG com os dados do erro
    echo '<h2>Error</h2><h3>Mensagem:</h3><pre>' . $exception->getMessage() . '</pre>
          <h3>Arquivo:</h3><pre>' . $exception->getLine() . '</pre>
          <h3>Linha:</h3><pre>' . $exception->getLine() . '</pre>
          <h3>Código:</h3><pre>' . $exception->getCode() . '</pre>';
}

set_exception_handler( 'myException' );

(set_error_handler)

Sets a user function to manipulate errors. When an error occurs, its function will identify and generate a log or trigger a Exception which will be captured by its above function myException.

function log_error( $code , $error , $file , $line )
{
    // obtendo informações do erro e disparando uma Exception
    if( error_reporting() === 0 ) return;
    new Exception( $error , $code , 0 , $file , $line );
}

set_error_handler( 'log_error' );

5

Use set_error_handler and register_shutdown_function to record the problems.

A few months ago I realized that I had several problems working with more than one developer (an average team), there were several errors that happened, so I made a code for this.

Try this (to save to files), include in all pages:

<?php
class getErrors
{
    static private $folder = 'data/erros/'; //Configure a pasta
    static private $writeOk = false;

    static public function writeErros($e) {//Erro personalizado
        if (self::$writeOk === true) {
            return NULL;
        }

        self::$writeOk = true;

        $log = fopen(self::$folder . gmdate('d-m-Y_H') . '-GMT.html', 'a+');

        if ($log) {
            $req = $_SERVER['HTTP_HOST'] . (
                isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']
            );
            fputs($log,
                '<meta http-equiv="Content-Type" content="text/html;">' .
                '<h1>Erro: ' . $e['message'] . '</h1>' .
                '<p>Página: <a href="http://' . $req . '">http://' . $req . '</a></p>' . 
                (isset($_SERVER['HTTP_REFERER']) === false ? '' : (
                    $_SERVER['HTTP_REFERER'] !== '' ? ('<p>Referer: <a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a></p>') : ''
                )).
                '<p>Linha: ' . $e['line']  .
                '<p>Arquivo: ' . $e['file']  .
                '<p>$_SERVER:<p><pre>' .
                print_r($_SERVER, true) . '</pre>' .
                '<p>$_GET:<p><pre>' .
                print_r($_GET, true) . '</pre>' .
                '<p>$_POST:<p><pre>' .
                print_r($_POST, true) . '</pre>' 
            );
            fclose($log);
        }
    }

    static public function putLastError() {
        $e = error_get_last();
        if (NULL !== $e) {
            self::writeErros($e);
        }
    }

    static public function handleErr($tipo, $errstr, $errfile, $errline, $detail) {
        self::writeErros(
            array(
                'message' => $tipo . ': ' . $errstr,
                'line'    => $errline,
                'file'    => $errfile
            )
        );
        return false;
    }
}

set_error_handler(array('getErrors', 'handleErr'), E_ALL|E_STRICT); //Configura a classe para o "handle"
register_shutdown_function(array('getErrors', 'putLastError')); //usar `error_get_last`, geralmente em erros "fatais"

Note: The function writeErros captures the variables used in the request so you can get an idea of what situation the problem occurred

1

You can set a function as callback if there are errors.

set_error_handler()

http://php.net/manual/en/function.set-error-handler.php

function LogErro($errno, $errstr, $errfile, $errline)
{
    //o que quiser...
}

set_error_handler("LogErro");

The Logerror() function will always be called when there are errors or warnings, and you have the error data in it to be able to handle them. See the explanation of the variables:

$Errno Error level, in int

$errstr Explanation of error

$errfile File where the error occurred

$errfile Line where error occurred

Browser other questions tagged

You are not signed in. Login or sign up in order to post.