Last active
April 11, 2018 16:29
-
-
Save dgpro/0011c6c1585701f160382b0f7727c67a to your computer and use it in GitHub Desktop.
PHP handle errors on shutdown and send by email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL & ~ E_NOTICE); | |
set_error_handler('my_error_handler'); | |
register_shutdown_function('send_error_log'); | |
$__errors = array(); | |
function my_error_handler($code, $message, $file, $line) | |
{ | |
if (!(error_reporting() & $code)) { | |
// This error code is not included in error_reporting | |
return; | |
} | |
global $__errors; | |
$__errors[] = sprintf('<b>%s</b> (%s line %s)', $message, $file, $line); | |
return; | |
} | |
function send_error_log() | |
{ | |
global $__errors; | |
if (!empty($__errors)) { | |
$body = count($__errors) . " Error(s):<br />"; | |
$body .= implode("<br>", $__errors); | |
$body .= "<hr> | |
Host: {$_SERVER['HTTP_HOST']}<br /> | |
Url: {$_SERVER['REQUEST_URI']}<br /> | |
IP: {$_SERVER['REMOTE_ADDR']}<br /> | |
User Agent: {$_SERVER['HTTP_USER_AGENT']}<br /> | |
"; | |
mail('[email protected]', "Error on {$_SERVER['HTTP_HOST']}", $body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment