In php/html/javascript there are two different ways you may redirect a user to another page however there are a few things to consider before choosing with method to use. As you should know by now php has the ability to send html and javascript to the web browser and php can contribute to html and javascript functionality by generating scripts on the fly. So first I shall demonstrate php's ability to generate scripts on the fly.
#method 1
<?php
$page='http://www.google.com/';
// $page=''; //will disable redirect
// some massave code above here
?>
<head>
<title>A test page</title>
<?php
if (strlen($page)>0) {
echo <<< DATA
<meta http-equiv="refresh" content="0;url={$page}">
<script>
window.location="{$page}";
</script>
DATA;
}
?>
</head>
Now in the above script you will see that it uses client side scripting for the redirect. In this case it uses both the meta redirect and javascript redirect so that it if javascript is disabled the browser can meta redirect or if meta redirect is disabled then the browser can javascript redirect. So if you want client side redirect that is the basic way to go about it keeping in mind client side redirecting isn't always the best option. Keep in mind that with client side redirecting, you are not guaranteed to make the user redirect. In some cases both javascript and meta redirect will be disabled. Luckily php has it's own solution. So lets see what php has installed for us.
#method 2
<?php
$page='http://www.google.com/';
// $page=''; //will disable redirect
if (strlen($page)>0) {
header('Location: '.$page); //no browser output before this line
exit;
}
?>
In this script you will see that it does exactly the same thing as the client side script except it's server side. To understand how this script works you simply insert the page you want to redirect to in the $page variable. If the $page variable is empty then the if statement will not pass meaning it will not execute the redirect. If however you have specified a page then it will add to the headers a redirect provided you haven't fed the browser any output or text. That means before the header function you can't even have the <html> tag outputted yet. It still has to be pure php at that point. And after the header redirect you should always follow it by the exit; statement as it will save php from executing the rest of the script (and you don't want that).
One might ask what the benefits of server side scripting a redirect are. Well the main reason is that it cannot be disabled and is more widely supported. Also header redirects require a lot less code provided they are used properly and if you really need html output before it then put your html output into a variable and echo that variable after the header function.
For example:
<?php
$text=<<< DATA
<head>
<title>Processing...</title>
</head>
<body>
This page is now processing...
DATA;
//html stuff in $text variable
$page='http://www.google.com/';
// $page=''; //will disable redirect
if (strlen($page)>0) {
header('Location: '.$page);
exit;
}
echo $text; //display html
?>
</body>
But as one may know that is not always the solution because you want to display something before something processes. That is where php has a neat function named popen(). I'm not going to say too much about the popen() function here as it is a redirect tutorial but what I will say is if you want to do a lot of processing an not want the user to wait for the redirect then you can open a background script with popen() and the user will have a nicer experience as they can browse around while they wait since they don't see a the page that is been executed. It's much like a cron job.
The conclusion:
Try to always use the header() function whenever possible even if it comes down to opening another thread with popen(). But only use javascript and meta tags as a last resort.