Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

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 no output has been sent to the browser. The first time that content is sent to the browser, PHP will send out headers and it will be 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 will cause 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.11 shows typical headers sent to the browser by PHP.

Listing 9.11 Typical Server Headers Sent from a PHP Script

1: HEAD /dev/php24/ch9/listing9.1.php HTTP/1.0
2: 
3: HTTP/1.1 200 OK
4: Date: Mon, 24 Sep 2001 14:32:28 GMT
5: Server: Apache/1.3.12 Cobalt (Unix) PHP/4.0.6 mod_perl/1.24
6: X-Powered-By: PHP/4.0.6
7: Connection: close
8: Content-Type: text/html

TIP

You can see headers sent in response to a request by using a telnet client. Connect to a Web host at port 80 and then type

HEAD /path/to/file.html HTTP/1.0

followed by two returns. The headers should be displayed on your client.

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

header( "Location: http://www.corrosive.co.uk" );

Assuming that we have 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.12.

Listing 9.12 Using header() to Send Raw Headers

 1: <?php
 2: $num_to_guess = 42;
 3: $message = "";
 4: $num_tries = ( isset( $num_tries ) ) ? ++$num_tries : 0;
 5: if ( ! isset( $guess ) )
 6:  $message = "Welcome to the guessing machine!";
 7: elseif  ( $guess > $num_to_guess )
 8:  $message = "$num is too big! Try a smaller number";
 9: elseif  ( $guess < $num_to_guess )
10:  $message = "$num is too small! Try a larger number";
11: else { // must be equivalent
12:  header( "Location: congrats.html" );
13:  exit;
14: }
15: $guess = (int)$guess;
16: ?>
17: <html>
18: <head>
19: <title>Listing 9.12 Using header() to send raw headers</title>
20: </head>
21: <body>
22: <h1>
23: <?php print $message ?>
24: </h1>
25: Guess number: <?php print $num_tries?>
26: <form method="POST">
27: Type your guess here:
28: <input type="text" name="guess" value="<?php print $guess?>">
29: <input type="hidden" name="num_tries" 
30:   value="<?php print $num_tries ?>">
31: </form>
32: </body>
33: </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.

  • + Share This
  • 🔖 Save To Your Account