0

In PHP is it possible to change the HTML of the White Page of Death (WPOD)? The goal is to do this change so that all untested (not prevented with an if-else) fatal errors would show this custom page.

For example when running the following would return a WPOD when the required filed doesn't exist:

require 'some-non-existing-file.php';

But I would like to return:

<!doctype html>
<html>
<head>
  <title>500</title>
  <style>body { background-color: #000; }</style>
</head>
<body></body>
</html>

I've tried to setup my <VirtualHost> to include and ErrorDocument directive:

ErrorDocument 500 /500.html

But according to this answer, after Apache starts to process the PHP, it won't trigger an Apache Error document anymore.

Community
  • 1
  • 1
grim
  • 6,669
  • 11
  • 38
  • 57
  • Use Nginx, and you can do this easily. – Brad Sep 25 '15 at 16:50
  • I'm not sure you can use a custom error document for a 500 error – Professor Abronsius Sep 25 '15 at 16:50
  • 1
    @RamRaider Of course you can. http://httpd.apache.org/docs/2.2/custom-error.html – Brad Sep 25 '15 at 16:51
  • fair enough - never got apache to serve a custom error page on a 500 error yet though – Professor Abronsius Sep 25 '15 at 16:53
  • 2
    500's are caused by things like syntax errors in your PHP doc.. why on earth would you even bother with creating a custom document for it.. just fix the error instead of worrying about what your users will see. – I wrestled a bear once. Sep 25 '15 at 16:53
  • @RamRaider it works with the ErrorDocument 500 "some message" directive in the VirtualHost/.htaccess but only for fatal errors that occurred in Apache (not PHP). – grim Sep 25 '15 at 17:04
  • thanks @grim - I just learned something :) – Professor Abronsius Sep 25 '15 at 17:07
  • 1
    @Adelphia I couldn't agree more for applications in simple ecosystems where the error is coming from the code; but when dealing with more complex infrastructures including multiple applications communicating together and going through multiple proxies/networks/servers on which you don't have full control, not all problems can be expected. Having a user friendly page as a backup is better than a WPOD. Furthermore, this question easily applies to other status codes. – grim Sep 25 '15 at 23:57
  • @grim, I see your point, sort of, but if you're getting data from an outside source that causes an error, Apache will still serve the broken page, not a 500. Likewise, Apache won't serve a 500 just because your script gets one from an outside source it's communicating with. AFAIK Apache will only throw a 500 if there's a misconfiguration with the server, (in which case, a custom doc is out of the question) or a simple PHP syntax error, in which case serving a custom 500 is basically a slap in the face to your users telling them, "it was a simple problem I'm just too lazy to fix it." – I wrestled a bear once. Sep 26 '15 at 00:24
  • @Adelphia Just as an example: how about if one of the outside sources is an API and the JSON response has a 200 status code but you weren't notified in advance that its format changed so your previous parsing won't work; it's a possibility, isn't it? Additionally, note that I'm not working on an application where users interact and there's a business rule that absolutely forbids the content to be completely white at any moment in time. Also, all status codes >= 400 are logged and a notification is sent to the dev team so it can be fixed promptly. – grim Sep 26 '15 at 01:00

2 Answers2

2

This doesn't affect the WSOD, but you could do some error handling with your PHP code that goes beyond if/else statements.

If you have code that you think could be causing problems, wrap it in a try/catch block like so:

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Then in the catch block you can print out the exception that is thrown by the code.

http://php.net/manual/en/language.exceptions.php

amklose
  • 1,143
  • 8
  • 15
2

You can check error using error handler

function checkErrorOnPage(){
  header("Location: 500.html");
}
set_error_handler('checkErrorOnPage');


require 'some-non-existing-file.php';
rocky
  • 631
  • 5
  • 14