PHP Manual
Prev |  | Next

# header

header -- Send a raw HTTP header

## Description

`int header`(string string);

The Header() function is used at the top of an HTML file to send raw HTTP header strings. See the HTTP 1.1 Specification for more information on raw http headers. Note: Remember that the Header() function must be called before any actual output is sent either by normal HTML tags or from PHP. It is a very common error to read code with include() or with auto_prepend and have spaces or empty lines in this code that force output before header() is called.

There are two special-case header calls. The first is the "Location" header. Not only does it send this header back to the browser, it also returns a REDIRECT status code to Apache. From a script writer's point of view this should not be important, but for people who understand Apache internals it is important to understand.

      1
      2 header ("Location: http://www.php.net"); /* Redirect browser
      3                                             to PHP web site */
      4 exit;                 /* Make sure that code below does
      5                          not get executed when we redirect. */
      6

The second special-case is any header that starts with the string, "HTTP/" (case is not significant). For example, if you have your ErrorDocument 404 Apache directive pointed to a PHP script, it would be a good idea to make sure that your PHP script is actually generating a 404. The first thing you do in your script should then be:

      1
      2 header ("http/1.0 404 Not Found");
      3

PHP scripts often generate dynamic HTML that must not be cached by the client browser or any proxy caches between the server and the client browser. Many proxies and clients can be forced to disable caching with

      1
      2 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
      3 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
      4                                                       // always modified
      5 header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
      6 header ("Pragma: no-cache");                          // HTTP/1.0
      7

Prev | Home | Next
HTTP functions | Up | setcookie