Saturday, January 5, 2008

PHP Redirect - How to Send Someone to Another URL With PHP


When designing a web site, it is occasionally necessary to redirect a user to another URL to the one they have tried to access. This can normally be accomplished using HTTP redirect, however sometimes they just are not applicable or you would prefer to do it with PHP, for example:

1. If a page is undergoing some maintenance and you wish to redirect users to an error page.

2. As part of a PHP conditional IF statement you want to redirect to another page, perhaps you want to check if a user is logged in yet and if not redirect to the login prompt.

I am sure the are many other reasons that escape me at the time of writing for why you may wish to do this.

Fortunately PHP does provide a means to allow web designers to take this control. This task is accomplished by manipulating the header of a web page before it has been served. In order to redirect a page to an alternative URL replace the contents of the file with:

Note that if any output has been written to the browser (including HTML tags) this approach will not work, it must be either the only or the first output in the file. Therefore if you want to use it as part of a conditional logic test put that at the start of the file, for example:

OPEN PHP

IF(loggedIn == TRUE){

CLOSE PHP

…HTML output can go here

OPEN PHP

}ELSE{

header( 'Location: http://www.yourURL.com' ) ;

}

CLOSE PHP

In order to view the code correctly in this article the OPEN PHP and CLOSE PHP tags are used instead of the actual start and end tags for PHP code - to run the code you need to substitute them for the correct open and close tags.

This technique is an easy way to control the flow of control in a browser based PHP application. Generally if you wish to always redirect one URL to another you should use a HTTP redirect, but a PHP redirect is also an option.
Dave Hodgson is a technical consultant by career and a website designer for fun. He has spent time working for large systems integrators, small consultancy firms and on individual freelance projects. The articles written by Dave are in the nature of tricks and tips he has learned through his career and interactions with clients.

By Dave Hodgson

3 comments:

aliens infotech said...

very nice....

thankyou!

by
Search engine optimization

Unknown said...

We add Life to your Vision. We listen to you, understand your ideas and thoughts with an endeavor to give you an outstanding web presence complete with the optimal solution for your business model.Technologies and CMS: (PHP, Cold Fusion, Zend, .NET, ASP, ASP.NET, or any CMS based websites).Thanks. Web Designing in India

Unknown said...

Nice blog.
Excellent blog.
Gorgeous blog.
I liked it.
its excellent
Really i loved read this blog.
Its highly informative. I would be visiting your blog hereafter regularly to gather valuable information.

You can also find interesting stuf here.

Kolkata Web Design and Search Engine Optimization