Php: 500 Error to Error Page

custom apache 500 error PHP page

Your comment is essentially correct. Many 500 errors do will not reach apache in a way that .htaccess will be able to redirect to an error document.

There are 2 ways that you can put out custom templates for 5xx errors. Which one you use will depend on exactly what the error is. If the error is "Catchable" You simply need to wrap your function in a try/catch block. Something like this:

try{
someUndefinedFunction();
} catch (Throwable $exception) { //Use Throwable to catch both errors and exceptions
header('HTTP/1.1 500 Internal Server Error'); //Tell the browser this is a 500 error
echo $e->getMessage();
}

Note that in this example, the 500 error header had to be set manually. This is because since the error is inside a try{} block the server didn't actually error out from the perspective of the browser.

If the 500 error is being caused by something that isn't catchable then you need to register a custom shutdown function. This is less common in php7+ but still may be necessary depending on what you're doing. The way that is done would be to include something like this:

function handle_fatal_error() {
$error = error_get_last();
if (is_array($error)) {
$errorCode = $error['type'] ?? 0;
$errorMsg = $error['message'] ?? '';
$file = $error['file'] ?? '';
$line = $error['line'] ?? null;

if ($errorCode > 0) {
handle_error($errorCode, $errorMessage, $file, $line);
}
}
}
function handle_error($code, $msg, $file, $line) {
echo $code . ': '. $msg . 'in ' . $file . 'on line ' . $line;
}
set_error_handler("handle_error");
register_shutdown_function('handle_fatal_error');

PHP: 500 Error to error page

Fatal errors don't produce 500 errors in and of themselves, they would return 200 with blank page typically (if no output had been flushed to browser at the point of the error) . Plus this will not help you anyway, as Apache would be no longer involved when PHP is having the error.

Maybe you could register a shutdown function to send 500 header (to get 500 result) and display the content you want to display.

php return 500 error but no error log

Scan your source files to find @.

From php documentation site

Currently the "@" error-control operator prefix will even disable
error reporting for critical errors that will terminate script
execution. Among other things, this means that if you use "@" to
suppress errors from a certain function and either it isn't available
or has been mistyped, the script will die right there with no
indication as to why.

Error for customize page error 500

Customized Error 500 pages are the hardest to use. The problem is, there are many reasons why a server can throw a 500 internal server error. Custom pages for 500 only work if the cause of the 500 can be handled by Apache to display the page. Say for instance you made a syntax error in your .htaccess page or in your apache config file, it's going to throw a 500 server error but because Apache is down, it can't serve anything including that custom page, so you get the browsers default page. Also depending on how you have your PHP error reporting setting you won't get 500 custom page either. There is a lot more to this and customizing 500 page is almost useless cause most people won't ever see that. You will know before they will because it's a 500 is typically configuration issue, like a syntax error in your code.

503 is the most common page a user will see especially if your site is down for maintenance and those are the one's I would focus on.

Anyway, I'd take a look at this link to see more info on 500 custom pages on when and why it won't work.

Apache's ErrorDocument directive does not redirect.

Also from Apache docs

Although most error messages can be overridden, there are certain
circumstances where the internal messages are used regardless of the
setting of ErrorDocument. In particular, if a malformed request is
detected, normal request processing will be immediately halted and the
internal error message returned. This is necessary to guard against
security problems caused by bad requests.

http://httpd.apache.org/docs/2.4/mod/core.html#errordocument

How can I make PHP display the error instead of giving me 500 Internal Server Error

Check the error_reporting, display_errors and display_startup_errors settings in your php.ini file. They should be set to E_ALL and "On" respectively (though you should not use display_errors on a production server, so disable this and use log_errors instead if/when you deploy it). You can also change these settings (except display_startup_errors) at the very beginning of your script to set them at runtime (though you may not catch all errors this way):

error_reporting(E_ALL);
ini_set('display_errors', 'On');

After that, restart server.



Related Topics



Leave a reply



Submit