Handle web page errors with PHP

Asked

Viewed 96 times

0

Hello! There is the possibility that when there is an error 404, for example, my server send an email notifying me of what happened through php?

  • Your server uses Apache?

  • Yes. I’m actually doing these tests through the wamp on my machine.

  • 2

    Virtually all websites on the internet are accessed by bots that scavenge vulnerabilities and end up falling on page 404. As a result you may receive hundreds if not thousands of emails. One tip is to only generate logs and then 1 time a day, in an automated scheduling, would be sent only 1 email with the report of the day’s logs. Anyway, it’s just advice. Follow whatever is convenient and suitable for your case.

  • Because it does not test Xampp is easy to use and better practice of developing PHP. If this web problem is may be port conflict, in windows 10 pro, try NET STOP HTTP is try restarting web server application. flw

1 answer

1

If you are using apache you can create a . htaccess file at the root with this content:

ErrorDocument 404 /home/user/public_html/enviar_erro.php

Assuming your "root" folder is public_html

The file has sent error.php:

<?php
$headers = "MIME-Version: 1.1\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: [email protected]\r\n"; // remetente

$mensagem = 'Url não encontrada: ' . $_SERVER['REQUEST_URI'] . PHP_EOL;

if (empty($_SERVER['HTTP_REFERER'])) {
    $mensagem .= 'Referencia: ' . $_SERVER['HTTP_REFERER'] . PHP_EOL;
}

mail("[email protected]", 'Erro HTTP', $mensagem, $headers);

echo '404 error';

And if you want to catch the script errors you can use as in this other answer /a/34818/3635, create a file called detectar_erro.php:

<?php
class getErrors
{
    //Seu email
    static private $enviarPara = '[email protected]';

    //Destinatário email
    static private $remetente = '[email protected]';

    static private $writeOk = false;

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

        self::$writeOk = true;

        $req = $_SERVER['HTTP_HOST'] . (
            isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] :
                                                    $_SERVER['PHP_SELF']
        );

        $mensagem = 'Erro: ' . $e['message'] . PHP_EOL .
                    'Página: http://' . $req . PHP_EOL .
                    (empty($_SERVER['HTTP_REFERER']) ? '' :
                        ('Referer: http://' . $req . PHP_EOL)
                    ) .
                    'Linha: ' . $e['line'] . PHP_EOL .
                    'Arquivo: ' . $e['file' PHP_EOL;

        $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
        $headers .= "From: " . self::$remetente . "\r\n"; // remetente

        mail(self::$email, 'Erro no PHP', $mensagem, $headers);
    }

    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;
    }
}

//Configura a classe para o "handle"
set_error_handler(array('getErrors', 'handleErr'), E_ALL|E_STRICT);

//usar `error_get_last`, geralmente em erros "fatais"
register_shutdown_function(array('getErrors', 'putLastError'));

And include at the top in all major files so:

<?php
require_once 'detectar_erro.php';

Browser other questions tagged

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