Error Handling

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
stuffandblah
Forum Newbie
Posts: 5
Joined: Thu May 28, 2009 3:56 am

Error Handling

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Error Handling

Post 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
stuffandblah
Forum Newbie
Posts: 5
Joined: Thu May 28, 2009 3:56 am

Re: Error Handling

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Error Handling

Post by Christopher »

(#10850)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Error Handling

Post 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)
...
stuffandblah
Forum Newbie
Posts: 5
Joined: Thu May 28, 2009 3:56 am

Re: Error Handling

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Error Handling

Post 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.
stuffandblah
Forum Newbie
Posts: 5
Joined: Thu May 28, 2009 3:56 am

Re: Error Handling

Post 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?!
stuffandblah
Forum Newbie
Posts: 5
Joined: Thu May 28, 2009 3:56 am

Re: Error Handling

Post 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
Post Reply