How can i trigger 500 Internal Server Error or 404 Page Not Found Apache errors in PHP?
For 500 Internal Server Error i have tried
I'm probably quite late, but this solution should work
header("HTTP/1.0 500 Internal Server Error");
include("/path-to-public-html/errors/500.php");
die();
This will trigger a 500 ISE response, and show the default 500 error page, then stop the script. Obviously you will need to to modify the location of the included file
Apparently there is a function called http_response_code that will let you set the response code appropriately. If that doesn't work (there's a note in the documentation about it possibly being only in SVN) then the header
function can take an optional response code argument:
header("HTTP/1.0 ...", true, 404);
This should signal Apache correctly.
On PHP 5.5 => "header("tester.php",true,500);" where tester.php does not exist. It works and takes me to my 500 page :)
Unfortunately none of the other methods listed here worked for me for one reason or the other.
After such a knowledge-full discussion, i think there is no php code can display the by default 500 Internal Server Error.
The solution is :
1. Create a folder named http500 next to the php file.
2. Create a .htaccess file in it and add following code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^])(.(*/
RewriteRule ^])((a-zA
</IfModule>
header('location : http500/');
I know this is an old thread however...
You could render an iframe element into your view, and then set its height and width to 100% in the event of the error you are preparing to handle.
<?php
header("HTTP/1.0 500 Internal Server Error");
http_response_code(500);
?>
<iframe src="page_displaying_apache_screenshot_fullscreen.html"></iframe>
THAT SAID
I truly think that the best modern solution for this would be something like:
<?php
header("HTTP/1.0 500 Internal Server Error");
http_response_code(500);
?>
<div class="panel-body">
<h1>Error!</h1>
<p>
<?php
// Then, in the view if your in dev env, print exception and stack trace
if ($env === 'dev')
{
throw new Exception('Uh oh! Something broke while adding a new user the db.');
}
else
{
echo "Hold your butts, an error has occured.";
die();
}
?>
</p>
</div>
This would result in a proper error code for all users, and also a stack trace for dev if you set the $env variable to 'dev' on your dev stack.
No redirect, no hackery, secure unless you need to expose an error it won't. :)
You could just copy the Apache error documents into your web root (or symlink them in) and use header() to set the error codes and with those documents as output. That's probably best unless you have a compelling reason not to. But in case that won't work for you, there are some alternatives (albeit, hacky alternatives).
For 500, just put a bug in your code.
<?php throw new Exception('Nooooooooooooooo!'); ?>
The PHP script will raise an exception, resulting in a regular Apache 500 error page. (But be aware of the caveat that this will only result in an Apache error if the PHP init setting display_errors
is set to 0
. In most development configurations, this is not the case. In most production configurations, this is the case. If you don't know your production server's setting, you'll have to just test this to see if it works.)
For 404, I think the best you can do is redirect to a non-existent page. It's not exactly the same thing, but might do for your purposes.
<?php header("Location: /i-dont-think-therefore-i-am-not"); ?>
In most browsers, the users won't actually see the redirect, they will just get silently and quickly redirected to a non-existent page, resulting in a regular Apache 404 error. Of course, the url will have changed and some users might notice that. If that's not acceptable, you can perhaps achieve similar results using Apache mod_rewrite to rewrite the url backend to a non-existent location.
To respond to several comments: Setting the response code via header()
(or some other way) will not result in the standard Apache error document. It will simply return that response code with your own output. This is intentional behavior, designed so that you can have custom error documents, or otherwise set response codes as the HTTP spec dictates on any request. If you want the Apache error documents, you have to either forge them (as per my initial suggestion of simply copying the error documents to your web root), or you'll have to trick Apache into returning them (as per my backup suggestion).