Redirecting Users

4.15 (55)


PHP has the capability of doing thousands of different useful things. On of those useful things, is the ability to redirect users using HTTP headers alone. This is different from using plain HTML Meta tags to redirect, as using this method the user won't even see the redirect occur. This can be very useful in creating a page that redirects to different pages, depending on the input to the script.

HTTP Headers

HTTP headers have been explained before on this site, however, I will explain it again, just to give another perspective on it. First and foremost, HTTP headers use the header() function. This is a very special function as it must come before anyway using input. The header() function takes three arguments:

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

From this, we can see the three arguments, being string, replace and http_response_code. For most of our purposes we will only use the first argument as the other two are optional. For reference though, replace is a true/false value which determines if this header overwrites previous similar headers or not. The http_response_code argument is used to send the response code to the browser, such as 404 for an error page.

As mentioned before, header() is very special, in that it must come before any other text / data is outputted to the screen. This is part of the HTTP protocol and doing it any other way, will result in an error and the header not working correctly. Hence, the following code is invalid:

Invalid Headers
echo 'Hello World';
header('Some header here…');

Obviously, as echo() prints out data to the screen, this makes this example invalid. Also, if HTML was present before the PHP tags, it would also be invalid. Now that that's explained, onto how to redirect users.

Redirecting using headers

To start off, we are going to do a very simple redirect to another page. This involves no conditional statements, nothing. So here it goes:

('Location: nextpage.php');

That was simple, wasn't it! This is very simple to explain as there is almost nothing to explain. The only thing to see is the notation of the header. We have Location: nextpage.php hence indicating, that we first use the Location keyword (this is the type of header), followed by a colon (:). Next we have the location of the page we want to redirect to (nextpage.php). This very simple code will cause the browser to redirect without the user even noticing. It also results in, if the user presses the 'Back' button in their browser, it brings them to the correct page, and does not cause one of those annoying redirect pages that can't be undone.

Complex Redirects

The page that PHP will redirect to does not have to be so simple. Often, we need to redirect the information that comes into a script, or simply redirect to another 'conditional' page. What I'm referring to, is the possibility of redirecting to PHP pages, with data in the URL:

('Location: nextpage.php?id=' $_POST['id']);

There is nothing too difficult about this one either, we simply add the form POST data onto the end of URL, as if we would normal. This script would redirect correctly, and the POST data would be passed along as GET data. Finally, check out conditional redirects.

Conditional Redirects

This topic is only marginally harder, as it involves conditional logic. Although, this is still very simple, there is a slight point you will have to notice if you want this script to work correctly. First, the script:

Conditional Redirects
if ($_POST['gametype'] == 'golf') {
header('Location: golf.php');
} elseif (
$_POST['gametype'] == 'soccer') {
header('Location: soccer.php');

if (
$_POST['user'] != 'Bob') {
header('Location: login.php'true);

header('Location: index.php'false);

Immediately it should be obvious, that we have used the second parameter of header(). Remember, this defines whether we overwrite similar headers or not, so in this case, it means if we overwrite previous redirects or not. We have set this to true as we want to make sure we redirect to login.php if the user is not correct. We also, have another redirect, with replace set to false. This is the 'catch all' redirect, as if none of the conditions are met, this is the redirect that will take place. We do not want it to overwrite previous redirects, so we set to false.

Other than that, the PHP code is fairly straight form, in that we have a few if/elseif statements with redirects inside. Only one of these redirects is going to happen when the data is sent to the form. That is all there is to HTTP header redirects and redirecting users. It can get more advanced if we were to use output buffering, however, that is completely unnecessary in most cases. Happy coding!
Rate this article: BAD 1 2 3 4 5   GOOD
Page 1 of 1

Build Your Own Database Driven Website Using PHP & MySQL

  • Installation instructions for Windows, Linux and Mac OS X
  • Instantly apply working code examples from the book to your Website
  • Build a working Content Management System from scratch
  • Master MySQL database administration
  • Fully updated for PHP 5

       Download FREESample Chapters Now!


PHPNerds Newsletter