Redirecting a user to a new location means that your script has sent an HTTP header to the browser, indicating a new location. HTTP headers of any kind (authentication, redirection, cookies, and so on) must be sent to the browser before anything else, including white space, line breaks, and any characters.
Although you've already used the header() function to redirect the user in the case of an incomplete form, in the next section you create a specific redirection menu. The goal is to have the users select a new location from a drop-down menu and then have the script automatically send them there.
In this section, you'll create the front end to a redirection script. This form will contain a drop-down list of the names of various websites. The value for each option is the website's URL.
Open a new file in your text editor and type the following HTML:
<HTML> <HEAD> <TITLE> Redirection Menu</TITLE> </HEAD> <BODY>
Begin your form. Assume that the method is POST and the action is a script called do_redirect.php:
<FORM METHOD="post" ACTION="do_redirect.php">
Add this drop-down list:
<P>Send me to:
<SELECT name="location">
<OPTION value="">-- Select One --</OPTION>
<OPTION value="http://www.thickbook.com/">thickbook.com</OPTION>
<OPTION value="http://www.i2ii.com/">i2i Interactive</OPTION>
<OPTION value="http://www.php.net/">PHP.net</OPTION>
<OPTION value="http://www.zend.com/">Zend Technologies</OPTION>
</SELECT>
Add a submit button:
<P><INPUT TYPE="submit" NAME="submit" VALUE="Go!"></P>
Close your form, and add some more HTML so that the document is valid:
</FORM> </BODY> </HTML>
Save the file with the name redirect_form.html, and place this file in the document root of your web server.
Open your web browser and type http://127.0.0.1/redirect_form.html.
In the next section, you create the back-end script. That script will expect one variable: $_POST[location].
According to the form action in redirect_form.html, you need a script called do_redirect.php. The goal of this script is to accept the value of $_POST[location] and print that value within the header() function so that the user is redirected to the chosen location.
Open a new file in your text editor and type the following PHP to create the proper redirection header, including a check to ensure that something was selected:
<?
if ($_POST[location] == ""){
header("Location: redirect_form.html");
exit;
} else {
header("Location: $_POST[location]");
exit;
}
?>
Save the file with the name do_redirect.php, and place this file in the document root of your web server.
Open your web browser and type http://127.0.0.1/redirect_form.html.
Select PHP.net from the drop-down list, and click on the Go! button.
Users are now redirected to the PHP website. Try some of the other selections, or add your own for practice.
In this chapter, you learned some of the different types of dynamic content that can be used in a website, and some other things you can do, such as redirecting users based on a drop-down menu. In the next chapter, you learn how to utilize of the more popular web tools—sending e-mail.