PHP header function usage and precautions, phpheader function details

Source: Internet
Author: User

PHP header function usage and precautions, phpheader function details

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.