This article mainly introduces the usage of the header function in PHP and relevant information about the precautions. it is of great reference value and can be referenced by anyone who needs it.
void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) : Send a raw HTTP header
Below are some usage of headers:
1. use the header function to jump to the page;
Header ('Location: '. $ url );
$ Url is the url to be redirected.
Note the following points for this usage:
• There must be no space between Location and ":"; otherwise, an error will occur (note: I just tested it. in my local environment, there is no page jump, but no error is reported, I don't know why );
• There cannot be any output before using the header (note: As we all know, if there is any output before the header, including blank, the error "header already sent by xxx" will appear );
• The content after the header will be executed;
2. use header to declare content-type
Header ('content-type: text/html; charset = utf-8 ');
There is nothing to say about it;
3. use the header to return the response status code
Header (sprintf ('% s % d % s', $ http_version, $ status_code, $ description ));
The style is like this;
Example: header ('http/1.1 404 Not Found ');
4. use the header to perform redirection after a certain time
Header ("Refresh: {$ delay}; url = {$ url }");
Where $ delay is the time to postpone the jump, $ url is the url to jump
For example: header ('refresh: 10; url = http://www.example.org/'); meaning jump to the http://www.eexample.org this site after 10 s
5. use the header to control browser cache
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache");
6. perform http verification
Header ('http/1.1 401 unauthorized ');
Header ('www-Authenticate: Basic realm = "Top Secret "');
7. use the header for download
Header ('content-Type: application/octet-stream'); // sets the Content Type header ('content-Disposition: attachment; filename = "example.zip "'); // Set the MIME user to download as an attachment. if you change attachment to inline, it means to open the header online ('content-Transfer-Encoding: bin '); // Set the transmission mode header ('content-Length: '.filesize('example.zip '); // set the Content Length // load the file to send: readfile('example.zip '); // read the object to be downloaded
The following describes several PHP header usage methods.
Jump page
Header ('Location: '. $ url); // There is no space between Location and.
Declare content-type
Header ('content-type: text/html; charset = utf-8 ');
Response status code
Header ('http/1.1 404 Not Found ');
Execute jump after a certain time
Header ('refresh: 10; url = http://www.baidu.com/'); // jump after 10 s.
Browser cache control
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
Header ("Cache-Control: no-cache, must-revalidate ");
Header ("Pragma: no-cache ");
Perform http verification
Header ('http/1.1 401 unauthorized ');
Header ('www-Authenticate: Basic realm = "Top Secret "');
Execute the download operation
Header ('content-Type: application/octet-stream'); // you can specify the Content Type.
Header ('content-Disposition: attachment; filename = "example.zip" '); // sets the MIME user as an attachment
Header ('content-Transfer-Encoding: binary '); // you can specify the transmission mode.
Header ('content-Length: '.filesize('example.zip '); // you can specify the Content Length.