PHP header () function
PHP HTTP functions
Definition and usage
The header () function sends the original HTTP header to the client.
It is important to recognize that the header () function must be called before any actual output is sent (in PHP 4 and later, you can use output caching to resolve this issue):
header(‘Location: http://www.example.com/‘); ?>
Grammar
Header (String,replace,http_response_code)
Parameters |
Description |
String |
Necessary. Specifies the string of headers to send. |
Replace |
Optional. Indicates whether the header replaces the previous header, or adds a second header. The default is true (replace). False (multiple headers of the same type are allowed). |
Http_response_code |
Optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and later) |
Hints and Notes
Note: After PHP 4.4, this function prevents multiple headers from being sent at once. This is a protective measure against head-injection attacks.
Example Example 1
<?php//Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Cache-Control: no-cache");header("Pragma: no-cache");
? >
Note: The user may set some options to change the browser's default cache settings. By sending the above header, you can override any of these settings to force the browser not to cache!
Example 2Prompts the user to save a generated PDF file (the content-disposition header is used to provide a recommended file name and to force the browser to display the Save dialog box):
<?php header("Content-type:application/pdf");
//file will be called Downloaded.pdf header("Content-Disposition:attachment;filename=‘downloaded.pdf‘");
//pdf Source in Original.pdf ReadFile ("original.pdf");? >
Note: Microsoft IE 5.5 has a bug that prevents the above mechanism. You can resolve the bug by upgrading to a version of Service Pack 2 or higher.
PHP header ()