Jump to content
Larry Ullman's Book Forums
banacan

Config.Inc.Php & My_Error_Handler

Recommended Posts

I have a site that uses config.inc.php, script 2.1 which has the error handler, my_error_handler. It has worked beautifully for me on many sites over the years but now I have a problem. My client has asked me to use a calendar script on the site, the script is EasyPHPCalendar. The problem is this script creates so many errors that my email box fills up - it causes 133 email error messages to be sent each time a page is loaded. Most of the errors are "undefined variables" or "use of undefined constant", there are also "undefined index" and "constant already defined" errors.

 

My question is how can I continue to use my_error_handler on this site without generating this plethora of errors? Right now I have commented out set_error_handler('my_error_handler') to avoid this problem.

 

The server php.ini file sets the error reporting to E_ALL & ~E_NOTICE,

the calendar script also has this: error_reporting(E_ALL ^ E_NOTICE);

 

Why am I getting these notices and how can I adjust the config.inc.php script to prevent this?

 

Many thanks.

Share this post


Link to post
Share on other sites

If the calendar script is only used in one page or a few, reference the URLs of those pages in an array and check inside the config file whether you are on that page then if you are on a calendar script page then don't invoke the custom error handler. Not ideal but would at least stop the dreaded emails, I hate it when i get 5 or 6 saying you messed up. Let alone 133

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks for the suggestion Jonathan. Unfortunately the client wants to have a short list of events in the sidebar of every page, so that work-around won't do.

 

Thanks for responding though.

Share this post


Link to post
Share on other sites

Well if yours is similar to mine

function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {

 

Can't you run a check to see the file that it came from? Wouldn't all the errors be from the same file? i.e calander.php (or whatever it is) regardless of the page?

  • Upvote 1

Share this post


Link to post
Share on other sites

Hey Jonathan,

 

I checked through the 133 error message emails and there are many different files responsible for the errors. However, the calendar script is all contained in one sub-directory, so do you think there is a way to exclude any errors created by files within a certain directory?

 

Thanks for helping.

Share this post


Link to post
Share on other sites

I'm sure the path of the file is returned in the error message, so maybe use a regex or substring inside an if to tell if the file came from that sub directory and if it did, don't invoke it?

  • Upvote 1

Share this post


Link to post
Share on other sites

Well that works great. Here's what I have:

 

// Create the error handler.
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
global $debug, $contact_email;

// Don't display errors found if $findme is in $e_file
$findme   = '/calendar/';
$found = strpos($e_file, $findme);
if ($found === false) {

 // Build the error message.
 $message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";

 //define timezone
 ini_set('date.timezone', 'America/Chicago');

 // Add the date and time.
 $message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";

 // Append $e_vars to the $message.
 $message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";

 if ($debug) { // Show the error.

  echo '<p class="error">' . $message . '</p>';

 } else {

  // Log the error:
  error_log ($message, 1, $contact_email); // Send email.

  // Only print an error message if the error isn't a notice or strict.
  if ( ($e_number != E_NOTICE) && ($e_number < 2048)) {
   echo '<p class="error">A system error occurred. We apologize for the inconvenience.</p>';
  }

 } // End of $debug IF.

} // End of $found IF.
} // End of my_error_handler() definition.

 

I tested it to make sure that only those messages which are generated by a file with "/calendar/" in the string are excluded. Done.

 

Thanks for your suggestion.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×