Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

On the top of my PHP page page04.php, I've this header:
PHP
<?php header('HTTP/1.1 403 Forbidden'); ?>

But when I go to page04.php with my browser, I don't get a 403 error page, but the contents of page04.php. I'm using an Apache HTTP server, and this is my .htaccess file:
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php

I tried also HTTP/1.0 instead of HTTP/1.1.
Why I don't get my 403 error page? And how can I configure the server, my .htaccess file or my page04.php file to get my 403 error page when I go to page04.php?

Thanks in advance.
Posted
Updated 29-Dec-12 5:23am
v3

1 solution

(A short description of this http status code: http://www.checkupdown.com/status/E403.html[^])

The problem is, that simply putting a 403 status code at the beginning of the page won't have any effect. The web server (Apache in your case) is deciding that the request is unauthorized, not your code. Since if it can execute the php script than it is authorized, right? So even if you send the 403 status code, you send the content also, and that's all, Apache won't intercept your status code. And the browser will be happy to display it :). What you set in .htaccess is only valid for Apache, php has nothing to do with it.

So, if you want to redirect from code to your error page, just add a http location[^] redirect after the status code, and exit immediately the php script.

If you want only to test the error page, just create a dummy folder and set .htaccess in it to deny everybody. If you made it correctly, you will get you error page back.
 
Share this answer
 
Comments
Thomas Daniels 29-Dec-12 12:38pm    
Good answer, thank you!
Zoltán Zörgő 29-Dec-12 12:54pm    
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900