The Developer Day | Staying Curious



PHP Fatal Errors Handling

Did you know that PHP custom made error handlers cannot handle Fatal errors or Parse errors. Those are the errors you would like to watch out for most! It’s not some warning or a notice that pops out and makes your visitor scratch his head. It’s an error that makes your visitor leave and sometimes never come back.

So now that we know that those errors are important to catch and there is no easy way to do it what should we do? Well actually there are some ways around to capture those kind of errors.

First way is kind of extreme, crazy and fun.. In php.ini you can find few options:
; String to output before an error message.
;error_prepend_string = "html code"
; String to output after an error message.
;error_append_string = "html code"

You could execute a simple JavaScript that would extract the fatal error from the document using regular expressions and innerHTML and send them to yourself using AJAX. and you could also display a very friendly message to the visitor by replacing the whole content of the site. The problem with this solution besides the fact that it’s fun is that it is ugly, might not work with every possible browser (hardly), and it requires you to setup a handler file that will accept those errors and send them to you. So you would have to do this on every single domain you have.

Other idea is to make use of a php.ini option error_log = filename. You can log all your php errors to a file. And you can write a cron script that would keep an eye on your file and watch for fatal errors. If it detects one it could easily report about it by sending an email or sending an SMS to your boss that he is losing customers! The cron script could also just empty the file when it gets too big.

I have read some rumors that fatal error handling will be available in PHP6 so there might one day be a proper way to do this. Though it may be that there are other existing solutions.

RSS Feed

6 Comments for PHP Fatal Errors Handling

The Developer Day » Blog Archive » PHP Fatal Errors Handling Continued | October 12, 2007 at 4:43 PM

[...] my previous post i wrote about few ideas how to capture php fatal errors. After some more research i found a few [...]

Matt C | June 19, 2009 at 11:27 AM

I know this is probably two years too late… but it occured to me that since you can put html before AND after your error, there is no need for crafty stuff like a regex… simply wrap the error in a div with an id.

Also something thats less problematic would be to add a cheap form to the pages document (via javascript of course) and post it to an error handling page. There the user is presented with the nice error and is really none the wiser for the flashy screen they get while the page reloads itself.

Matt C | June 19, 2009 at 11:30 AM

On a side note… I’ll likely be setting this up for myself in the next day or so. I’d be willing to share it since you pointed out the key php config that game me the idea.

anon | August 26, 2009 at 10:05 AM

The simple solution is output buffering and regex, the error messages are easy enough to match. There are security implications to enabling error output which don’t go away if the error message reaches the client.

Afnan | December 22, 2009 at 5:53 AM

Zend Server studio (enterprise) do this by default and email us the exact backtrace of the fatal error, But it’s expensive.

one can have community version for free.

Stan | January 21, 2013 at 10:10 AM

In case of Parse error or Fatal Error the script stops exection so we can use register_shutdown_function(user_defined_function) for handling parse and fatal error

Leave a comment!



Find it!

Theme Design by