2

Possible Duplicate:
How to make PHP set HTTP status code to 500 automatically in case of any error condition? (including those that cannot be handled by user)

I want to configure Apache to send to the user a 500 response with a custom 500 page when a PHP script raises a fatal error. What I have now is that it will print the error inline with the page content which is good for development but not for production.

How do I configure Apache to send the user a 500 response with a custom page?

Community
  • 1
  • 1
Uriel Katz
  • 187
  • 1
  • 2
  • 10

3 Answers3

1

Assuming that you don't only want to send a 500 header (which is easy), but really trigger a 500 error in Apache's system, there seems to be no trivial solution.

According to this question, it is PHP's default behaviour since PHP 5.2.4 if:

  1. a fatal error occurs and
  2. the document body is empty (Gordon found the changelog entry here).

I'm not sure how reliable this behaviour is long term (i.e. when PHP 6 comes up etc.). It's not a much advertised feature.

Other than that, I know of no way of provoking a 500 once the script runs. I asked something similar for 404s once. The answer provided there (redirecting to a predefined URL, and sending a 500 header) may be the best you can get - although that of course won't be logged.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

I would say that

ErrorDocument 500 /some/page.php

should do the trick, or just a nice HTML page if you want to be sure that it renders properly even when strange things are preventing PHP pages to render.

In order to trigger the error page you look at this anser : How to return an HTTP 500 code on any error, no matter what

Community
  • 1
  • 1
Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
0

I want to do the same thing but I don't think it is possible with the current version of PHP. First, there is a bug where 500 errors return 200 status. So setting the ErrorDocument 500 in your config file will have no effect. In addition, there appears to be no way to access error information (for custom display and logging) even if it could be redirected.

Triggering an error is easy though. Just add this to a PHP file:

throw new Exception();
Chris Broski
  • 2,421
  • 27
  • 23