Jump to content

Default Error Response


cyber0ne

Recommended Posts

Supposing I have a vhost in apache that's hosting an entirely php-driven website, is there a way to have any and all fatal errors (or any class of error that causes the php script to stop executing) present the user with a standard catch-all error page customized for that vhost?Basically, I'd just like to have a default "I'm sorry, but an error occurred" page with some contact info, the company logo, etc. and have all errors for the site send the user to that page.Any ideas?

Link to comment
Share on other sites

The easiest way is probably to use your own custom error handler. It can give whatever information you want to show about the error, and whatever additional information you want to show. See the below page for information about error handling. One thing to take notice of on that page is the list of configuration options. You could probably use a combination of the error_prepend_string and error_append_string options to display HTML before and after the error message, but it will still display the error message. See the large example on that page for examples about how to set your own error handler. If all you want to do is redirect to another page (with no information on it about the error), then just put a header redirection in the error handler, or else you can have it display some extra information about the error.http://www.php.net/manual/en/ref.errorfunc.php

Link to comment
Share on other sites

That looks really useful, thanks!I have a question about the bitmask for those constants and how it's applied in the code. In the example, I see:

// we will do our own error handlingerror_reporting(0);

Is this where that bitmask is being applied? Does the 0 mean that any and all of the aforementioned constants will be automatically caught by this error handler?For example, suppose somewhere in my code I forget to check for the existance of a file before using it in a require() statement. By default, the script halts execution and an error is written to the vhost's error log. With this code in place, would that behavior be pre-empted by the custom error handler?

Link to comment
Share on other sites

According to this page:http://www.php.net/manual/en/function.error-reporting.phpThe parameter to error_reporting() is a 13-bit number. Bit 1 corresponds to E_ERROR, bit 2 corresponds to E_WARNING, bit 3 to E_PARSE, bit 4 to E_NOTICE, etc. So, if you wanted to only report errors, warnings, and notices, the bit string would look like this:0000000001011Or, integer value 11. If you give error_reporting a value of 0, it corresponds to this bit string:0000000000000meaning that all errors are disabled. So, this statement:error_reporting(0);disabled all automatic error reporting. But this does not automatically send it to your custom handler. You still need to use set_error_handler to set your own handler:http://www.php.net/manual/en/function.set-error-handler.phpYou can leave errors enabled and also set your own handler, and in that case the user will still see an error message, and your custom handler will also be invoked (maybe the handler sends an email, or creates a log, or whatever instead of notifying the user). You will want to read that reference page for set_error_handler, there's good info there. Such as this:

Also note that it is your responsibility to die() if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error.
Some errors will mean that your script just cannot continue, so you will need to explicitly check for that and end the script yourself if necessary. There's also this:
The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called. If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.
Obviously parse errors cannot be handled, because if your file has a parse error then it won't even be executed at all, including setting the error handler. The same goes with the fatal E_ERROR. This indicates things like a problem with memory allocation or something else that is fatal, and the script won't be able to continue running regardless of what your handler does. The core and compiler errors aren't anything you need to worry about, because those would be errors with the PHP install itself, not necessarily with your application.With regard to the require statement, this would raise an E_ERROR fatal error and so the application would stop, regardless of your handler. That is the difference between require and include. Require means the script cannot proceed without the file, and include will instead generate E_WARNING and will allow you to catch and handle the error.
Link to comment
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
×
×
  • Create New...