PHP header() Function

The header() function sends a raw HTTP header to a client. It is important to notice 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) : -

ParameterDescription
stringRequired. Specifies the header string to send
replaceOptional. Indicates whether the header should replace previous or add a second header. Default is TRUE (will replace). FALSE (allows multiple headers of the same type)
http_response_codeOptional. Forces the HTTP response code to the specified value (available in PHP 4.3 and higher)

Since PHP 4.4 this function prevents more than one header to be sent at once. This is a protection against header injection attacks.

Example

There are options that users may set to change the browser's default caching settings. By sending the headers above, you should override any of those settings and force the browser to not cache!

Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box) : -

Example

There is a bug in Microsoft IE 5.5 that prevents this from working. The bug can be resolved by upgrading to Service Pack 2 or later.