Redirection using PHP
Joel Philip on 2002 April 06
    

Have you ever seen a webpage that, instead of loading you the information you expect, takes you "magically" elsewhere? I'm sure you have. Well, that "magic" is actually called redirection. Similar to those implemented in HTML but server-side, and in our case using (what else?) PHP. This article will briefly teach you it's implementaion.



Redirection in PHP is possible thanks to the header() function. Its simple to use and you
can have your friends redirected to all kinds of places.

Let's start with a simple page called agreement.php that has an agreement on it and uses two ways out: "I agree" and "I disagree".

Here is the HTML code for agreement.php:



<HTML>
<HEAD>
<TITLE>Welcome to my site</TITLE>
</HEAD>
<BODY> 

<h1>Welcome to my site</h1>
<p>In order to enter my site you must 
<br>agree to these temrs and conditions:

<p>terms .... bla bla bla...

<p>

<center>
<a href="agreement.php?answer=ok">I agree</a> &nbsp; &nbsp; | &nbsp; &nbsp;
<a href="agreement.php?answer=nope">I disagree</a>
</center>

</BODY>
</HTML>



Obviously, after our user read (yeah, right) our agreement he or she can whether agree on it by clicking "I agree" link or disagree by hitting the alternative link. However, both links are pointing to the same page just with a different value in the answer variable. That is because we don't want to let user go anywhere else unless we know what he/she think about this agreement.



So user clicked, what's now?
Let's check for user's choice:


<?php
if($answer=='ok') {
    
header("Location: http://www.yoursite.com/whatever_page.html");
}
elseif(
$answer=='nope') {
    
header("Location: $HTTP_REFERER");
}
elseif(!
$answer) {
    
// Default HTML code
}


?>


We've just used header() function to open so a new location. In this case it is the web site named
"yoursite.com/whatever_page.html". Obviously when user clicked "I disagree" we've thrown him into the original location where he came from. But, we could also give him a chance to change his mind by pointing him/her a whole new location. How? Just change $HTTP_REFERER to some other site/page name:


<?php
    
// on "I disagree"
    
header ("Location: http://www.other_site.com/bye-bye.html");

?>


This is a simple script but is very powerful. For example, you could even make a form with no fields
at all but just a submit button to do the same thing. Just place the script's name in the action
section of the form.

Just remember that a header() function must be used at the top of your script
before any code can be sent to the browser. This is a common mistake for most of the beginners.
NO whitespaces, blank lines or anything that could be sent to your browser before header() is called.



Bad examples of using header()

PHP does not start from the first line:
------------


<?php
        
header
("Location: http://site.com/");

?>

------------

An output before header()
------------

<?php
echo "hello world";            // no echo
header("Location: http://site.com/");

?>

------------

In oher words: your script MUST NOT output anything before header() is called. FYI: same thing for sessions.

Full code for the "agreement.php"


<?php
if($answer=='ok') {

    
// on agree proceed to whatever page
    
header("Location: http://www.yoursite.com/whatever_page.html");

}
elseif(
$answer=='nope') {

    
// on disagree return to the previous page
    
header("Location: $HTTP_REFERER");

}
elseif(!
$answer) {

    
// Default HTML code

    
echo "<HTML>n";
    echo 
"<HEAD><TITLE>Welcome to my site</TITLE>n";
    echo 
"</HEAD>n";
    echo 
"<BODY>n";

    echo 
"<h1>Welcome to my site</h1>n";
    echo 
"<P>In order to enter my site you must n";
    echo 
"<BR>agree to these temrs and conditions:n";

    echo 
"<p>terms .... bla bla bla...n";

    echo 
"<P>n";

    echo 
"<center>n";
    echo 
"<a href="agreement.php?answer=ok">I agree</a> &nbsp; &nbsp; | &nbsp; &nbsp;n";
    echo 
"<a href="agreement.php?answer=nope">I disagree</a>n";
    echo 
"</center>n";

    echo 
"</BODY>n";
    echo 
"</HTML>n";

}


?>



Hopefully, this little example will save you some time and headache redirecting your users to right places by using the power of PHP.
Tentatively planning to Open Soon! (no dates ...) // Doing heavy development now...