views:

62

answers:

4

I have a script in production - an ecommerce checkout page - that has had some errors in the past that have prevented it from working and have cost me money. I wanted to get notified on errors so I worked this up:

<?php
function mailErrorHandler($errno, $errstr)
  {
  echo "<!--PHP ERROR:";
  echo "---[$errno] $errstr ---";
  echo "-->";
  error_log("Error: [$errno] $errstr",1,
  "[email protected]","From: [email protected]");
  }

set_error_handler("mailErrorHandler",E_ALL);

echo 1-thisisnotanumber;

?>

When I use it as-is in it's own script, it works and executes quickly. However, when I add it to my existing application, the page load time decreases DRAMATICALLY i.e. 40 seconds as opposed to <1 second. Can anyone think of a reason why this might be happening?

A: 

Hmm. When you say "use it on it's own", do you mean in a separate page that is invoked through Apache, or are you running it on the command line? The delay, combined with the use of email makes me suspect a DNS or network issue...something not resolving or not connecting and timing out.

Another thought...fire up Xdebug and do a profile dump while this is running and see if that sheds any light on what is taking all the time.

Peter Loron
What I mean is that by saving the above into it's own php file and accessing it in my web browser, the script execution time is dramatically lower than the time added to my actual application when I include the code above in it.
Bruce the Hoon
A: 

Try to add an exit() after the error_log() call.

powtac
A: 

An other solution would be to log to a file if you have problems with the error_hander:

Set

log_errors = On
html_errors = Off
error_log = log

in your php.ini, then all the errors will be logged into the default error.log of your server.

powtac
+1  A: 

If you have a significant amount of traffic and you're throwing a LOT of errors, writing to the log can cause a significant amount of disk IO. This can slow down your app to the extent that you're talking about.

Maybe what you're throwing isn't errors, but rather a bunch of Notice "exceptions". If you have them set to not display (the default in most versions of PHP) and you're getting a boat load of them, you could be running your error handler hundreds and hundreds of times. Every time the handler is run, it has to do a trace, break out of the current scope, do all sorts of processing, and if that's all happening because you're using =& new with PHP 5.3 or trying to access undefined array elements (or any other common notice), you're going to see those kinds of delays.

So in order to fix this, the doctor prescribes turning off the error handler on your test server, turning on the display of notices, run through the flow and take note of any errors/notices/etc, then fix the aforementioned notices on your production box.

Hope this helps!

mattbasta
Agree, if every pageload trow up 10 errors, and for each of them the error_handler have to send out an email, the 40'sec delay can be a send issue.Check out your smtp server or, my advice, edit your error handler in order to trace all the errors, but send just 1 email, at the end of the script.
DaNieL