views:

1024

answers:

4

Hi have created a error.php for joomla to redirect 404 errors etc to a joomla article. The code below works for a 404 error, but a 403 returns a blank page. I can navigate to the page directly outside my script so it must be either my code or how it is interactive in it's environment.

Thanks Stephen

defined( '_JEXEC' ) or die( 'Restricted access' );
if ($this->error->code == 404)
{
Header( "HTTP/1.1 404 Not found" );
header("Location: http://www.queensberry.com/misc/filenotfound/");
} else if ($this->error->code == 403) {
Header( "HTTP/1.1 403 Restricted Content" );
Header( "Location: http://www.queensberry.com/restrictedcontent/" ); 
} else {
echo "hello world error code = " . $this->error->code;
}
?>
+3  A: 

Rather than using Header("Loaction:...") you should be rendering the "restricted content" page with an include() or some such and then exiting. The browser isn't following the Location header after receiving a 403 most likely.

gnarf
Thanks Steven and gnarf.gnarf are you saying that instead of waiting to be bounced of the restricted page like I am now, I should be editing that page to out my error message or control the redirect from there>Not sure I completely understand.CheersStephen
Stephen Baugh
I use a framework, so my 404/403 is a little different, but all my experience tells me your problem would go away if you just had some sort of `403.php` file that you could include(`403.php`); and it would just spit out the error page directly. I think that is the expected behavior of the server to display an error page when giving an error code.
gnarf
A: 

The Header("Location:") is causing a standard redirect to a new page. The 403 error will only be sent if it is on the page rendered by PHP. So you're effectively trying to say (with the code you posted) "The current page results in a 403 error" but then you redirect to an entirely different page altogether. Add the 403 header to http://www.queensberry.com/restrictedcontent/ and you should be good. You also need to do the same with the 404 header as well.

Steven Surowiec
+1  A: 

I appreciate everyone's help, but the answers were sending me in a different direction to what I was hoping for. I wanted to continue using Joomla's error.php file as the destination for Joomla errors but instead of formatting the page to look like it was part of the site I wanted to redirect to Joomla content.

In the end I found what I needed was an exit; in my script. So here is the error.php as it is now working.

defined( '_JEXEC' ) or die( 'Restricted access' );
if ($this->error->code == 404)
{
Header( "HTTP/1.1 404 Not found" );
header("Location: http://www.queensberry.com/misc/filenotfound/");
exit;
} else if ($this->error->code == 403) {
Header( "HTTP/1.1 403 Restricted Content" );
Header( "Location: http://www.queensberry.com/restrictedcontent/" ); 
exit;
} else {
echo "hello world error code = " . $this->error->code;
}
?>
Stephen Baugh
A: 

If (HTTP/1.1 403) then Web Browser (Firefox, IE, Chrome, ...) will ignore whatever

Location: (bla bla bla) is.

Do not believe ?

Assume :: you know how to produce Error Code 403 from your Joomla Site. (I mean joomla_site/bla-bla-bla :: that produce Error Code 403)

Let's ask whether Web Server gives (based on your error.php file) as follows :

... HTTP/1.1 403 Restricted Content Location: http://www.queensberry.com/restrictedcontent/ ...

Those are right based on your error.php file (I mean your error.php file works as you wish)

-- Check with telnet 80

Let's try

c:\telnet your_host:80

GET bla-bla-bla HTTP/1.1

Host: joomla_site

-- Hope this fulfill your curiosity :-)

I repeat it again :

If (HTTP/1.1 403) then Web Browser (Firefox, IE, Chrome, ...) will ignore whatever

Location: (bla bla bla) is.

joomla-bali