Skip to content

Instantly share code, notes, and snippets.

@dgpro
Last active April 11, 2018 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgpro/0011c6c1585701f160382b0f7727c67a to your computer and use it in GitHub Desktop.
Save dgpro/0011c6c1585701f160382b0f7727c67a to your computer and use it in GitHub Desktop.
PHP handle errors on shutdown and send by email
<?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('my@email.com', "Error on {$_SERVER['HTTP_HOST']}", $body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment