The header() function sends a raw HTTP header to the client. header(string,replace,http_response_code) It is important to see that header() must be sent before any actual output is called
PHP header function tutorial
Definition and usage
The header() function sends a raw HTTP header to the client.
It's important to see that headers ( ) must be sent before any actual output is called (in PHP 4 and later, you can use output buffering to solve this problem):
// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?>
Syntax:
header(string,replace,http_response_code)
|
Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
string | Required. The specified header string is sent | ||||||||
replace | Optional. Indicates whether the title should replace the previous one or add a second title. The default value is true (will be replaced). false (allows multiple titles of the same type) | ||||||||
http_response_code | Optional. Forces the HTTP response code to a specified value (available in PHP 4.3 and higher) |
Tips and Instructions NOTE: As of PHP 4.4 this feature prevents more than one header from being sent at once. This is a protection against header injection attacks. Example 1 Prevent page caching:
// Date in the pastheader("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Cache-Control: no-cache");header("Pragma: no-cache") ;
Example 2 Let the user be prompted to save the generated PDF file (the content-disposition header is used to provide suggested filenames and force the browser to show the save dialog):
header("Content-type:application/pdf");
// It will be called downloaded.pdfheader("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source is in original.pdfreadfile("original.pdf");