Help - Search - Members - Calendar
Full Version: [PHP] - [Error Reporting in your inbox] - [Gio]
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Gio
Well ever have a visitor complain about a page being down or something like this? Well with this tutorial, anytime an error appears on your site on any page you put this code on, it will e-mail the error directly to your inbox. So let's get down to business.

CODE
<?
// Decide what errors to report:
error_reporting (E_ERROR | E_WARNING | E_NOTICE);
// Webmaster email and error page url:
$Wmemail = 'you@ursite.com';
$ErrorPage = 'http://www.ursite.com/error.html';
// This function will check the error type:
function MailErrorHandler($errno, $errstr, $errfile='?', $errline= '?')
{
global $Wmemail, $ErrorPage;
if (($errno & error_reporting()) == 0)
return;
$err = '';
switch($errno) {
case E_ERROR: $err = 'FATAL'; break;
case E_WARNING: $err = 'ERROR'; break;
case E_NOTICE: return;
}
// Send the error details via email:
mail($Wmemail,
"PHP: $errfile, $errline",
"$errfile, Line $errline\n$err($errno)\n$errstr");
// Redirect to the error page:
print '<META HTTP-EQUIV="Refresh" CONTENT="0;url='.$ErrorPage.'">';
die();
}
set_error_handler('MailErrorHandler');
?>


It is commented so that the code explains it's self, however I will explain it alittle bit so this isnt just a code dump.

CODE
<?
// Decide what errors to report:
error_reporting (E_ERROR | E_WARNING | E_NOTICE);
// Webmaster email and error page url:
$Wmemail = 'you@ursite.com';
$ErrorPage = 'http://www.ursite.com/error.html';
// This function will check the error type:
function MailErrorHandler($errno, $errstr, $errfile='?', $errline= '?')
{


Ok this part gets your e-mail address and the url to your error page so it can redirect if the user encounters an error. Then it gets all the details of your error so it can email it to you.

CODE
global $Wmemail, $ErrorPage;
if (($errno & error_reporting()) == 0)
return;
$err = '';
switch($errno) {
case E_ERROR: $err = 'FATAL'; break;
case E_WARNING: $err = 'ERROR'; break;
case E_NOTICE: return;
}


Gets the error details and the severity then gets ready to redirect to the error page url.

CODE
// Send the error details via email:
mail($Wmemail,
"PHP: $errfile, $errline",
"$errfile, Line $errline\n$err($errno)\n$errstr");
// Redirect to the error page:
print '<META HTTP-EQUIV="Refresh" CONTENT="0;url='.$ErrorPage.'">';
die();
}
set_error_handler('MailErrorHandler');
?>


This part actually sends the email to your inbox, includes the details of the error, the page url the output the error, what line the error was caused on and all that jazz. Then it redirects the user to the error page.

Well that is all, hope you enjoyed this, and I hope that it benefits you all in some way or another.
Anarchy
nice, this will help out a lot of people victory.gif
Gio
I sure hope so. If not I wasted my time sad.gif
Jay
post php initiated errors are cause the user stepped out of bounds or cause they did something they shouldn't have.

but i guess i can see the use in this if its something big gone wrong, nice tutorial
Gio
Thanks, and actually to get a compliment from you makes it better. I figured you would hate it lol.
Jaymz
Thanks smile.gif In use on my site right now...
Gio
Have you gotten any errors in your inbox yet?
jaywintermeyer
How can i make this script catch parse errors?
Gio
Parse errors appear on there own, if something is wrong the page displays that info on its own. But as for collecting this parse error info and sending it, I am not sure if it can be done.
mike_go
I've seen duzens of functions like this flying arround on the web, but they all fail because set_error_handler() can't handle the error type E_ERROR and a few other.

So don't waste your time like I did.

From php.net site:
"The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called."

Documentation:
http://www.php.net/manual/en/function.set-error-handler.php
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.