Redirecting the User

Our simple script still has one major drawbackthe form is reloaded 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 absolutely sure that no output has been sent to the browser. The first time content is sent to the browser, PHP sends out headers of its own, and it's too late for you to send any more. Any output from your document, even a line break or a space outside 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 11.8 shows typical headers sent to the browser by PHP, beginning with line 3, in response to the request in line 1.

Listing 11.8. Typical Server Headers Sent from a PHP Script

 1: HTTP/1.1 200 OK 2: Date: Tue,  28 Feb 2006 07:17:28 PST 3: Server: Apache/2.0.55 (Win32) PHP/5.1.2 4: X-Powered-By: PHP/5.1.2 5: Connection: close 6: Content-Type: text/html

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


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 11.9. The only change between this and Listing 11.7 comes after the else clause on line 10.

Listing 11.9. Using header() to Redirect User

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

The else clause of our if statement on line 10 now causes the browser to send us away to a page called congrats.html. We ensure that all output from the current page is aborted with the exit statement on line 12, which immediately ends execution and output of this script.

Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327 © 2008-2017.
If you may any questions please contact us: