Home arrow PHP arrow Page 4 - HTTP Headers in Web Development

Examples of HTTP Header Usage - PHP

At this point, you may not know what HTTP headers are, but you use them all the time, both as an Internet user and as a web developer, even if you are not aware of it. The beginning of this article is a short introduction to HTTP headers, including what they are, and what they do. Later in the article, we cover some common uses of HTTP headers in web development.

  1. HTTP Headers in Web Development
  2. What do HTTP headers look like?
  3. How Headers Are Used
  4. Examples of HTTP Header Usage
By: John Best
Rating: starstarstarstarstar / 14
August 26, 2008

print this article



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:

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:

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:

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded_file.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.


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: