PHP
  Home arrow PHP arrow Page 4 - HTTP Headers in Web Development
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

HTTP Headers in Web Development
By: John Best
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2008-08-26


    Table of Contents:
  • HTTP Headers in Web Development
  • What do HTTP headers look like?
  • How Headers Are Used
  • Examples of HTTP Header Usage

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    HTTP Headers in Web Development - Examples of HTTP Header Usage
    ( Page 4 of 4 )

    Web Page to Browser

    If you have done much scripting with PHP, you have probably used a line like this

      header('Location: http://www.somesite.com/page.php');

    in your scripts to redirect the browser to a different page. The part inside the parentheses is actually an HTTP header request header. The PHP header function can be used in the same manner with any http header. The URL specified in the location header above must be an absolute URL. It is a good idea to follow the header function with exit(); in a PHP script, to ensure that the code does not continue to execute after the redirect.

    PHP programmers sometimes find that the location header or some other header function fails. The likely reason for this is that it is preceded by browser output. The request header must always be the very first thing that is sent to the browser. The PHP header function sends the header to the browser in the same order as it occurs in the PHP code. Therefore, if the PHP code outputs anything to the browser, even something as minor as a blank line or a cookie, before the code gets to the line containing the header function, the header written by the header function will fail because it wasn't part of the header's first output to the browser.

    Fortunately for PHP programmers, there are the ob_start() and ob_end_flush() functions which cause the output to the browser to be cached until all of that output for the entire page is assembled, eliminating this problem. The "ob" stands for "output buffering."

    ob_start() goes before the beginning of any browser output, and ob_end_flush() goes at the end of the output. For example:

    <?php 
    ob_start(); //begin buffering the output

     
    echo "This is the first browser output to be buffered";  
    header('Location: http://www.somesite.com/page.php');

    ob_flush(); //output the data in the buffer
    ?>

    As you can see, this would not have worked without the output buffering functions because output would have already been sent to the browser before the header() function. Alternatively, the output_buffering configuration directive can be set in the php.ini or server configuration files.

    The Refresh: http header can also be used with the PHP header() function, to redirect the user after a time delay. This example provides a three second delay:

    header('Refresh: 3; url= http://www.somesite.com/pagetwo.php ');

    Another very common use of http headers is to prevent browsers from caching the page. This code snippet can be used to prevent it:


    <?php
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

    header('Pragma: no-cache'); // HTTP/1.0 (old browsers)
    ?>

    Still another use is to force a "Save File" dialog box with a recommended file name, for downloading a file, using the Content-Disposition: header as in this example:

    <?php
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="downloaded_file.pdf"');
    readfile('file_on_server.pdf');
    ?>

    Between Web Pages

    Another area of HTTP header usage is communication between web pages. Request headers are often used for this purpose. At the beginning of this article, you saw the GET method used. Another one you may be familiar with is POST. There are several others, but those two are probably the most useful in web development. The biggest use is "under the hood" in form submissions, which as you know, utilize the GET or POST methods.

    Conclusion

    In this article, we barely scratched the surface of any aspect of HTTP headers and their usage. For more in depth information on this useful subject, I refer you to the W3C Standard RFC 2616 and to an excellent article in Wikipedia on the subject that contains links to further resources.



     
     
    >>> More PHP Articles          >>> More By John Best
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek