Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP header() Function

PHP header() Function


PHP HTTP Reference Manual Complete PHP HTTP Reference Manual


Definition and Usage

The header() function sends a raw HTTP header to the client.

It is important to note that header() must be called before any actual output is sent (in PHP 4 and later, you can use output buffering to solve this problem):

Syntax

header(string,replace,http_response_code)

Parameters Description
string Required. Specifies the header string to send.
replace Optional. Indicates whether this header should replace a previous header or add a second header. Default is TRUE (replace). FALSE (allows multiple headers of the same type).
http_response_code Optional. Forces the HTTP response code to a specified value. (Available in PHP 4.3 and later)

Tips and Notes

**Note:** Since PHP 4.4, this function prevents sending multiple headers at once. This is a protection against header injection attacks.


Example 1

Disable page caching:

...

...

**Note:** Users may set options that change the browser's default caching settings. By sending the headers above, you can override any of these settings and force the browser not to cache!


Example 2

Prompt the user to save a generated PDF file (the Content-Disposition header is used to provide a recommended file name and force the browser to display a save dialog):

...

...

**Note:** Microsoft IE 5.5 has a bug that prevents the above mechanism. Upgrading to Service Pack 2 or later resolves this bug.


PHP HTTP Reference Manual Complete PHP HTTP Reference Manual