Page 161 - Open Soource Technologies 304.indd
P. 161

Open Source Technologies



                   Notes         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 shows typical headers sent to
                                 the browser by PHP, beginning with line 3, in response to the request in line.
                                 Listing 9 Typical Server Headers Sent from a PHP Script

                                    1.  HEAD/listing9.php
                                    2.  HTTP/1.0

                                    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.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:
                                 header(“Location: http://www.samspublishing.com”);

                                 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 10.
                                 Listing 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”;




        156                               LOVELY PROFESSIONAL UNIVERSITY
   156   157   158   159   160   161   162   163   164   165   166