Redirecting the User

Our simple script still has one major drawback. The form is rewritten whether or not the user guesses correctly. The fact that the HTML is hard-coded makes it difficult to avoid writing the entire page. We can, however, redirect the user to a congratulations page, thereby sidestepping the issue altogether.

When a server script communicates with a client, it must first send some headers that provide information about the document to follow. PHP usually handles this for you automatically, but you can choose to send your own header lines with PHP's header() function.

To call the header() function, you must be sure that absolutely no output has been sent to the browser. The first time content is sent to the browser, PHP sends out headers and it's too late for you to send your own. Any output from your document, even a line break or a space outside of your script tags, causes headers to be sent. If you intend to use the header() function in a script, you must make certain that nothing precedes the PHP code that contains the function call. You should also check any libraries that you might be using.

Listing 9.9 shows typical headers sent to the browser by PHP, beginning with line 3, in response to the request in line 1.

Listing 9.9 Typical Server Headers Sent from a PHP Script
   1: HEAD /listing9.9.php HTTP/1.0   2:    3: HTTP/1.1 200 OK   4: Date: Sun, 15 Sep 2002 12:32:28 GMT   5: Server: Apache/2.0.43 (Unix) PHP/4.2.3 mod_ssl/2.8.9 OpenSSL/0.9.6   6: X-Powered-By: PHP/4.2.3   7: Connection: close   8: Content-Type: text/html 

By sending a "Location" header instead of PHP's default, you can cause the browser to be redirected to a new page:


Assuming that we've created a suitably upbeat page called "congrats.html", we can amend our number-guessing script to redirect the user if she guesses correctly, as shown in Listing 9.10.

Listing 9.10 Using header() to Send Raw Headers
   1: <?php   2: $num_to_guess = 42;   3: $num_tries = (isset($_POST[num_tries])) ? $num_tries + 1: 0;   4: $message = "";   5: if (!isset($_POST[guess])) {   6:    $message = "Welcome to the guessing machine!";   7: } elseif ($_POST[guess] > $num_to_guess) {   8:    $message = "$_POST[guess] is too big! Try a smaller number";   9: } elseif ($_POST[guess] < $num_to_guess) {  10:    $message = "$_POST[guess] is too small! Try a larger number";  11: } else { // must be equivalent  12:    header("Location: congrats.html");  13:    exit;  14: }  15: $guess = $_POST[guess];  16: ?>  17: <html>  18: <head>  19: <title>Listing 9.10 Saving state with a hidden field</title>  20: </head>  21: <body>  22: <h1>  23: <?php print $message ?>  24: </h1>  25: Guess number: <?php print $num_tries?>  26: <form action="<?php print $_SERVER[PHP_SELF] ?>" method="POST">  27: Type your guess here:  28: <input type="text" name="guess" value="<?php print $guess?>">  29: <input type="hidden" name="num_tries" value="<?php print $num_tries?>">  30: </form>  31: </body>  32: </html> 

The else clause of our if statement on line 11 now causes the browser to request congrats.html. We ensure that all output from the current page is aborted with the exit statement on line 13, which immediately ends execution and output, whether HTML or PHP.

Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263 © 2008-2017.
If you may any questions please contact us: