PHP header function tutorial
Definition and usage
The header () function sends an original HTTP header to the client.
It is important to see that the title () must be sent in any so-called actual output (after PHP 4 and later, you can use the output buffer to solve this problem ):
<? Php
// 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)
Parameter |
Description |
String |
Required. The specified title string is sent.
|
Replace |
Optional. Indicates whether the title should replace the previous or add the second title. The default value is true (will be replaced ). False (multiple titles of the same type are allowed)
|
Http_response_code |
Optional. The HTTP response code of the troops to the specified value (available in PHP 4.3 and higher)
|
Tips and instructions
Note: The PHP 4.4 feature prevents more than one title from being sent 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
Ask the user to save the generated PDF file (the content disposition title is used to provide a suggested file name and force the browser to display the save dialog box ):
Header ("Content-type: application/pdf ");
// It will be called downloaded.pdf header ("Content-Disposition: attachment?filename='downloaded= '");
// The PDF source is in original.pdf readfile ("original.pdf ");