Error Handling
Moderator: General Moderators
-
stuffandblah
- Forum Newbie
- Posts: 5
- Joined: Thu May 28, 2009 3:56 am
Error Handling
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
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
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
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
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Error Handling
(#10850)
Re: Error Handling
PHP has been throwing 500 on fatal errors since v5.2.4: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 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
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
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
It does, really, but only if display_errors php.ini setting is turned off. Why it so is beyond my understanding.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.
-
stuffandblah
- Forum Newbie
- Posts: 5
- Joined: Thu May 28, 2009 3:56 am
Re: Error Handling
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?!
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
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
The same also happens on my linux host, I just get a blank page, even when the .htaccess file is: ErrorDocument 500 "moo"
Dave