Jump to content



Photo

Config.Inc.Php & My_Error_Handler


  • Please log in to reply
7 replies to this topic

#1 banacan

banacan

    Member

  • Members
  • PipPip
  • 48 posts

Posted 15 November 2011 - 1:18 PM

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.
  • 0

#2 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 15 November 2011 - 1:35 PM

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
  • 1

#3 banacan

banacan

    Member

  • Members
  • PipPip
  • 48 posts

Posted 15 November 2011 - 4:05 PM

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.
  • 0

#4 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 15 November 2011 - 4:39 PM

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?
  • 1

#5 banacan

banacan

    Member

  • Members
  • PipPip
  • 48 posts

Posted 16 November 2011 - 9:11 AM

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.
  • 0

#6 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 16 November 2011 - 10:07 AM

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?
  • 1

#7 banacan

banacan

    Member

  • Members
  • PipPip
  • 48 posts

Posted 16 November 2011 - 12:18 PM

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.
  • 0

#8 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 16 November 2011 - 2:54 PM

No problem, glad it works for you.
  • 1