I know this is an old thread, but when I searched on Google, it jumps out as the first answer. I was trying to figure out how to do it these days, so I think I should post my workaround from 2018.
As you all have known, no, you cannot simply trigger Apache’s error pages from PHP. There’s no way doing it. So the best workaround I could find, after some research, is to use a PHP function to display the custom error pages, which is called from both what you specified in Apache, and the page that you want to trigger 500 Internal Server Error.
Here is my conf
of Apache:
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
As you could see, all 4xx and 500 errors are redirected to a same php file. Why I’m not using separate files for each code is another question, which has nothing to do with your question here, so let’s forget it and focus on error.php
for now.
Here are some lines of error.php
:
<?php
require_once("error_page.php");
$relative_path = "";
$internal_server_error = FALSE;
if (isset($_SERVER['REDIRECT_URL']) && $_SERVER['REDIRECT_URL'] != "")
{
$relative_path = $_SERVER['REDIRECT_URL'];
$internal_server_error = (http_response_code() >= 500);
}
else if (isset($_SERVER['REQUEST_URI']))
{
$relative_path = $_SERVER['REQUEST_URI'];
}
ErrorPage::GenerateErrorPage($internal_server_error, $relative_path);
?>
In short, it receives HTTP status code from Apache via http_response_code()
, and simply categorize the status code into two categories: Internal Server Error and Non-Internal Server Error. It then passes the category to ErrorPage::GenerateErrorPage()
which actually generates contents of the error page.
In ErrorPage::GenerateErrorPage()
, I do something like this:
<?php
http_response_code($internal_server_error ? 500 : 404);
?>
at the beginning of the function, so that there won’t be any annoying “headers already sent”. This function will generate a fully functional error page with correct (or what we expected, at least) HTTP status code. After setting the HTTP status code, as you may guess, one of two prepared contents will be generated by following php codes, one for 500, another for 404.
Now let’s go back to your question.
You want to trigger exactly the same error page as Apache does, and now you can simply call the ErrorPage::GenerateErrorPage()
from wherever you want, as long as you give it correct parameters (TRUE
for 500, FALSE
for 404 in my case).
Obviously, because that the Apache error pages are generated by the same function ErrorPage::GenerateErrorPage()
, we can guarantee that what you trigger wherever is exactly what you would expect from Apache error pages.
Here is my example of a trigger page:
<?php
ob_start();
require_once("error_page.php");
try
{
// Actual contents go here
phpInfo();
throw new Exception('test exception');
}
catch (Exception $e)
{
ob_clean();
ErrorPage::GenerateErrorPage(TRUE);
ob_end_flush();
exit();
}
ob_end_flush();
?>
The reason I use ob_start()
and ob_clean()
is that, in this way, I can clear anything output before the exception occurs, and send a pure 500 Internal Server Error page that looks exactly the same as Apache gives, without any content on the trigger page (phpInfo()
in this case). ob_start()
and ob_clean()
also make sure that there won’t be “headers already sent”, because nothing will ever be sent unless you call ob_end_flush()
.
The solution above is concluded from what I researched these days. It is so far so good for me, but I’m not sure if it is the correct way to do it. Any comments, questions, thoughts, suggestions and improvements are welcomed.
/usr/share/httpd/error/HTTP_INTERNAL_SERVER_ERROR.html.var
for me) and exit the PHP script. But I'm having a huge problem: how would you go about processing the .html.var file? In Apache it's set asAddHandler type-map var
but I can't figure out anything that would be useful in PHP. – animuson♦