tags:

views:

39

answers:

2

I wonder what patterns are there for handling errors in PHP?

I'm close to just make an global ErrorManager object which has methods to add messages. As my entire app gets compiled, any occuring error is logged to that ErrorManager object. At the end of my Front Controller (the last few lines of the script) I tell the ErrorManager to display errors (if any). One fancy thing I thought about: Visitors really don't care about technical details. Instead they should receive an beautiful error page with a big "SORRY" and an info that the error was reported. Then, the ErrorManager sends an E-Mail to the admin with all available information and logs that stuff to a error log file. This file can be seen through the backend, to make development easy.

No idea if this is a good strategy. I bet you know better ones :-)

+3  A: 

You should never ever let any visitors see any technical error messages. They don't need to see it and it is a security flaw. If a user discovers a sql injection, showing the sql error message makes it easier to them to work out your database structure.

Regarding emailing, you want to be very careful with how you do it, as the admin may be asleep when the error starts occurring, and end up with 20,000 emails in his inbox by the time he or she wakes up.

A better idea is to use a log file or database and send a single email a hour (or day) when an error occurs. That way at maximum there is 12 emails about errors.

Yacoby
good point with emails!
openfrog