Home >Backend Development >PHP Tutorial >PHP header function implementation code_PHP tutorial
PHP header() function definition and usage The header() function sends raw HTTP headers to the client. It's important to realize that the header() function must be called before any actual output is sent (in PHP 4 and above, you can use output caching to solve this problem):
php tutorial header() function
Definition and usage
The header() function sends raw http headers to the client.
It is important to realize that the header() function must be called before any actual output is sent (in PHP 4 and above, you can use output caching to solve this problem):
location
refresh
content-type
expires (meaning to terminate)
last-modified etc.
location – The browser displays the specified web page.
Format: header(“location: http://absolute address”);
The address of location must be an absolute address.
refresh
refresh – update the web page.
// The result is wrong
// Output already exists before calling header()
header('location: http://www.bkjia.com/');
?>Syntax
header(string,replace,http_response_code)
This function prevents multiple headers from being sent at once
// date in the past
header("expires: mon, 26 jul 1997 05:00:00 gmt");
header("cache-control: no-cache");
header("pragma: no-cache");
?>
Prompt the user to save a generated pdf file (the content-disposition header is used to provide a recommended filename and force the browser to display a save dialog):
When the time exceeds the specified time, it means that the content of the web page has expired. Its format is as follows
expires = “expires:” http-date
For example: header(“expires: fri, 31 oct 2003 18:00:00 gmt”); means 18:00 on Friday, October 31, 2003.
header(“expires: wed, 30 jun 2004 09:00:00 gmt”); indicates Wednesday, June 30, 2004 at 9:00.
last-modified = “last-modified” “:” http-date
For example header(“last-modified: wed, 01 jan 2003 12:00:00 gmt”); last modified at 12:00 on January 1, 2003
cache control
no-cache allows server data to be sent to remote users without being cached.
no-store is to prevent bad versions or sensitive information from being retained.
Must-revalidate means that the information must be re-evaluated. If the cached data is invalid, the server and the user will be connected
date refers to the creation date and time of the data content. Its format is as follows: date = “date:” http-date
Example: header(“date: sun, 15 feb 2004 08:00:00 gmt”); The creation date is 8:00 on February 15, 2004.
header("content-type:application/pdf");//The file will be called downloaded.pdf
header("content-disposition:attachment;filename='downloaded.pdf'");// pdf source is in original.pdf
readfile("original.pdf");
?>