Jump to content


- - - - -

[PHP] - [Error Reporting in your inbox] - [Gio]


  • Please log in to reply
9 replies to this topic

#1 _*Gio_*

_*Gio_*
  • Guests

Posted 27 August 2004 - 08:31 AM

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.

<?
// Decide what errors to report: 
error_reporting (E_ERROR | E_WARNING | E_NOTICE);
// Webmaster email and error page url:
$Wmemail = '[email protected]';
$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.

<?
// Decide what errors to report: 
error_reporting (E_ERROR | E_WARNING | E_NOTICE);
// Webmaster email and error page url:
$Wmemail = '[email protected]';
$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.

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.

// 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.

#2 _*Anarchy_*

_*Anarchy_*
  • Guests

Posted 27 August 2004 - 12:23 PM

nice, this will help out a lot of people :o

#3 _*Gio_*

_*Gio_*
  • Guests

Posted 27 August 2004 - 02:47 PM

I sure hope so. If not I wasted my time :o

#4 _*Jay_*

_*Jay_*
  • Guests

Posted 27 August 2004 - 05:17 PM

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

#5 _*Gio_*

_*Gio_*
  • Guests

Posted 27 August 2004 - 08:00 PM

Thanks, and actually to get a compliment from you makes it better. I figured you would hate it lol.

#6 _*Jaymz_*

_*Jaymz_*
  • Guests

Posted 04 September 2004 - 11:28 PM

Thanks :D In use on my site right now...

#7 _*Gio_*

_*Gio_*
  • Guests

Posted 09 September 2004 - 03:35 PM

Have you gotten any errors in your inbox yet?

#8 jaywintermeyer

jaywintermeyer

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 27 September 2004 - 09:40 AM

How can i make this script catch parse errors?

#9 Gio

Gio

    Jedi In Training

  • Members
  • PipPip
  • 317 posts

Posted 27 September 2004 - 09:57 AM

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.

#10 mike_go

mike_go

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 10 June 2005 - 08:08 PM

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/m...ror-handler.php




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users