Page 1 of 1

Error Handling

Posted: Thu May 28, 2009 4:02 am
by stuffandblah
Hi guys,

Having come from an ASP.NET background, I wondered if there is any way of handling errors in a friendly way, such as redirecting to a custom error page? ASP.NET does this by setting paramaters in the web.config file and I was hoping that errors, such as fatal ones, could be managed under PHP.

Is there a best practice way of dealing with these (apart from not producing the error in the first place)?

Cheers

Dave

Re: Error Handling

Posted: Thu May 28, 2009 4:48 am
by onion2k
You can do the equivalent by setting some directives in Apache's .htaccess config files.. eg

Code: Select all

ErrorDocument 404 /notfound.html
ErrorDocument 500 /internalservererror.html

Re: Error Handling

Posted: Thu May 28, 2009 11:04 am
by stuffandblah
Thanks onion2k. I forgot about Apache, although this does pose another issue...

When calling an undefined function a fatal error is created, however, the HTTP response code is 200 and not 500. Do you have any ideas as to how this can be forced into responding as a 500 code?

My plan was to show a friendly page on error, but also capture the error contents to a log file (including additional data).

Dave

Re: Error Handling

Posted: Thu May 28, 2009 11:32 am
by Christopher

Re: Error Handling

Posted: Thu May 28, 2009 12:08 pm
by Weirdan
stuffandblah wrote:When calling an undefined function a fatal error is created, however, the HTTP response code is 200 and not 500. Do you have any ideas as to how this can be forced into responding as a 500 code?
PHP has been throwing 500 on fatal errors since v5.2.4:
PHP changelog wrote: Version 5.2.4

30-August-2007

...
Changed error handler to send HTTP 500 instead of blank page on PHP errors. (Dmitry, Andrei Nigmatulin)
...

Re: Error Handling

Posted: Thu May 28, 2009 12:36 pm
by stuffandblah
OK, I'm a little confused now. Both my local setup using xampp and my linux host both return response codes 200 instead of 500 when a fatal error occurs (non existent function), so I'm not too sure about PHP doing that as standard.

Thanks for the info re: set_error_handler. Which I will use for all other errors, however, this will not cater for fatal errors.

I found this, which may be of interest: http://www.phpbuilder.com/board/showthr ... p=10895403

Although it still leaves me wondering as to how to handle fatal errors :(

Thanks for your support people (one of the reasons why I love PHP and the community that goes with it)

Dave

Re: Error Handling

Posted: Thu May 28, 2009 1:05 pm
by Weirdan
stuffandblah wrote:OK, I'm a little confused now. Both my local setup using xampp and my linux host both return response codes 200 instead of 500 when a fatal error occurs (non existent function), so I'm not too sure about PHP doing that as standard.
It does, really, but only if display_errors php.ini setting is turned off. Why it so is beyond my understanding.

Re: Error Handling

Posted: Thu May 28, 2009 1:32 pm
by stuffandblah
Ok, so I set: ini_set('display_errors', 0); which causes the 500 response (thanks).

Although now, my .htaccess, which has:
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

Doesn't pick the 500 error up.

my 500.php file (in the root) just contains a basic echo('500');

The 404, does work. So the .htaccess is working. Weird?!

Re: Error Handling

Posted: Fri May 29, 2009 3:20 am
by stuffandblah
Has anyone else had problems with 500 errors not being picked up by Apache?

The same also happens on my linux host, I just get a blank page, even when the .htaccess file is: ErrorDocument 500 "moo"

Dave