Page 532 - Beginning PHP 5.3
P. 532

Part III: Using PHP in Practice
                    header()  is very useful if your PHP script needs to send anything other than an HTML Web page. For
                 example, say you have a   report.pdf  file on the Web server, and you want to send this to the browser.
                 You could write the following:

                      < ?php
                    header( “Content-Type: application/pdf” );
                    readfile( “report.pdf” );
                    ? >

                   The first line tells the Web browser to expect a PDF document rather than a regular Web page. The
                 second line reads the PDF file on the server ’ s hard drive and outputs its contents to the Web browser,
                 which can then save or display the PDF.
                  Usually it ’ s up to the browser as to whether it displays the file in the browser itself, or offers to save it to
                 the user ’ s hard disk. You can use a   Content - Disposition: Attachment  header to suggest to the
                browser that the file should be saved rather than displayed and, optionally, to suggest a filename for the
                saved file:
                      < ?php
                    header( “Content-Type: application/pdf” );
                    header( ‘Content-Disposition: attachment; filename=”Latest Report.pdf”’ );
                    readfile( “report.pdf” );
                    ? >

                   By the way, you need to make sure you don ’ t send anything to the browser before calling  header() .
                 This includes HTML markup, or even blank lines, before your opening    < ?php  tag. This is because, once
                 PHP has received a request to send some content to the browser, it sends the HTTP headers first (because
                 the headers need to be sent at the start of the response). Therefore, by the time your   header()  call is
                 executed, the content is already being sent, and it ’ s too late to send any more headers. (If you fall foul of

                 this, then your header isn ’ t sent and PHP generates a   Cannot modify header information  -
                 headers already sent  warning.)


                  Getting Information from the Web Server

                   Each time a PHP script is run, the Web server software makes a wealth of useful information available to
                 the PHP engine. Such information includes details about the server itself, as well as details of the script
                 being executed, and many of the HTTP request headers discussed previously in this chapter.
                   You can access all of this information in your PHP script through the   $_SERVER  superglobal array. For
                 example, to display the IP address of the visitor ’ s computer (or proxy server) you might use:


                    echo “Your IP address is: “ . $_SERVER[“REMOTE_ADDR”];
                   Because Web servers come in all shapes and sizes, the information available depends very much on your
                 particular server setup. Having said that, there ’ s usually a core list of values that are always present.
                 Here ’ s a list of the more common and useful   $_SERVER  variables:






              494





                                                                                                      9/21/09   9:15:37 AM
          c16.indd   494                                                                              9/21/09   9:15:37 AM
          c16.indd   494
   527   528   529   530   531   532   533   534   535   536   537