Emulate a 403 error page

PhpHeaderHttp Status-Code-403

Php Problem Overview


I know you can send a header that tells the browser this page is forbidden like:

header('HTTP/1.0 403 Forbidden');

But how can I also display the custom error page that has been created on the server for this type of error?

By default, just sending the header displays a white page, but I remember a while back reading that you can use the customer error page. Does anybody know?

Php Solutions


Solution 1 - Php

Just echo your content after sending the header.

header('HTTP/1.0 403 Forbidden');

echo 'You are forbidden!';

forbidden

Solution 2 - Php

http_response_code was introduced in PHP 5.4 and made the things a lot easier!

http_response_code(403);
die('Forbidden');

Solution 3 - Php

Include the custom error page after changing the header.

Solution 4 - Php

For this you must first say for the browser that the user receive an error 403. For this you can use this code:

header("HTTP/1.1 403 Forbidden" );

Then, the script send "error, error, error, error, error.......", so you must stop it. You can use

exit;

With this two lines the server send an error and stop the script.

Don't forget : that emulate the error, but you must set it in a .htaccess file, with

ErrorDocument 403 /error403.php

Solution 5 - Php

Seen a lot of the answers, but the correct one is to provide the full options for the header function call as per the php manual

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

If you invoke with

header('HTTP/1.0 403 Forbidden', true, 403);

the normal behavior of HTTP 403 as configured with Apache or any other server would follow.

Solution 6 - Php

I have read all the answers here and none of them was complete answer for my situation (which is exactly the same in this question) so here is how I gathered some parts of the suggested answers and come up with the exact solution:

  1. Land on your server's real 403 page. (Go to a forbidden URL on your server, or go to any 403 page you like)

  2. Right-click and select 'view source'. Select all the source and save it to file on your domain like: http://domain.com/403.html

  3. now go to your real forbidden page (or a forbidden situation in some part of your php) example: http://domain.com/members/this_is_forbidden.php

  4. echo this code below before any HTML output or header! (even a whitespace will cause PHP to send HTML/TEXT HTTP Header and it won't work) The code below should be your first line!

    	<?php header('HTTP/1.0 403 Forbidden');
    	$contents = file_get_contents('/home/your_account/public_html/domain.com/403.html', TRUE);
    	exit($contents);
    

Now you have the exact solution. I checked and verified with CPANEL Latest Visitors and it is registered as exact 403 event.

Solution 7 - Php

.htaccess

ErrorDocument 403     /403.html

Solution 8 - Php

To minimize the duty of the server make it simple:

.htaccess

ErrorDocument 403 "Forbidden"

PHP

header('HTTP/1.0 403 Forbidden');

die(); // or your message: die('Forbidden');

Solution 9 - Php

Use ModRewrite:

RewriteRule ^403.html$ - [F]

Just make sure you create a blank document called "403.html" in your www root or you'll get a 404 error instead of 403.

Solution 10 - Php

I understand you have a scenario with ErrorDocument already defined within your apache conf or .htaccess and want to make those pages appear when manually sending a 4xx status code via php.

Unfortunately this is not possible with common methods because php sends header directly to user's browser (not to Apache web server) whereas ErrorDocument is a display handler for http status generated from Apache.

Solution 11 - Php

Refresh the page after sending the 403:

<?php 
header('HTTP/1.0 403 Forbidden');
?>
<html><head>
<meta http-equiv="refresh" content="0;URL=http://my.error.page">
</head><body></body></html>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNightHawkView Question on Stackoverflow
Solution 1 - PhpalexView Answer on Stackoverflow
Solution 2 - PhpMarcio MazzucatoView Answer on Stackoverflow
Solution 3 - PhpIbrahim AshShohailView Answer on Stackoverflow
Solution 4 - PhpPyrrhaView Answer on Stackoverflow
Solution 5 - PhpJiju Thomas MathewView Answer on Stackoverflow
Solution 6 - PhpTarikView Answer on Stackoverflow
Solution 7 - Phpuser557846View Answer on Stackoverflow
Solution 8 - Phpvirtual_ciaView Answer on Stackoverflow
Solution 9 - PhpJay SudoView Answer on Stackoverflow
Solution 10 - PhplabemiView Answer on Stackoverflow
Solution 11 - PhpRichardView Answer on Stackoverflow