PHP header function usage and precautions, phpheader function explanation _ PHP Tutorial

Source: Internet
Author: User
The usage and precautions of the header function in PHP are described in detail, and the phpheader function is described in detail. The usage and precautions of the header function in PHP are described in detail. voidheader (string $ string [, bool $ replacetrue [, int $ http_response_code]) of the phpheader function is described in detail. the usage and precautions of the header function in SendarawHTTPh PHP are described in detail, and the phpheader function is described in detail.
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.

Protected void header (string $ string [, bool $ replace = true [, int $ http_response_code]): Send a raw HTTP h...

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.