The usage of header function in PHP and its notice _php example

Source: Internet
Author: User
Tags browser cache
void header (String $string [, bool $replace = true [, int $http _response_code]]): Send a raw HTTP header

Here are several uses of header:

1, use header function to jump page;

Header (' Location: '. $url);

Where $url is the URL that will jump.

  The following considerations for this usage are:

location and ":" Can not have a space between, or there will be errors (note: I just tested, in my local environment, no jump page, but there is no error, not clear what the reason);

• No output before using header (note: This is known to all, if there is any output before header, including blank, there will be header already sent by xxx error);

The things behind the header will also be carried out;

2. Use Header statement Content-type

Header (' Content-type:text/html;charset=utf-8 ');
There is nothing to say about it;

3, use header return response status code

Header (sprintf ('%s%d%s ', $http _version, $status _code, $description));

The style is like this;

For example: Header (' http/1.1 404 Not Found ');

4. Use header to perform jump after a certain time

Header ("Refresh: {$delay}; url={$url} ");

Where $delay is the time to postpone the jump, $url to the URL that needs to jump

For example: Header (' refresh:10; url=http://www.example.org/'); Meaning 10s jump to http://www.eexample.org this site

5. Use header to control browser cache

Header ("Expires:mon, June June 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 validation

Header (' http/1.1 401 Unauthorized ');
Header (' Www-authenticate:basic realm= "top Secret");

7. Use Header for download operation

Header (' Content-type:application/octet-stream ');/set the content type
header (' content-disposition:attachment; Filename= ') Example.zip "');  Set MIME user to download as an attachment if you change the attachment to inline meaning to open
header (' content-transfer-encoding:binary ') online, set the transmission mode
Header (' Content-length: '. FileSize (' Example.zip '))//Set Content length
//load the file to send:
ReadFile (' Example.zip ' //Read files that need to be downloaded

Here are a few more examples of PHP header usage

Jump page

Header (' Location: '. $url); There are no spaces between location and ":".

Statement Content-type

Header (' Content-type:text/html;charset=utf-8 ');

Return response status Code

Header (' http/1.1 404 Not Found ');

To perform a jump after a certain time

Header (' refresh:10; url=http://www.baidu.com/'); 10s after the jump.

controlling browser caching

Header ("Expires:mon, June June 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");

Performing HTTP validation

Header (' http/1.1 401 Unauthorized ');
Header (' Www-authenticate:basic realm= "top Secret");

Perform a download operation

Header (' Content-type:application/octet-stream '); Set Content Type
Header (' content-disposition:attachment; filename= "Example.zip"); Set MIME user as Attachment
Header (' content-transfer-encoding:binary '); Set transport mode
Header (' Content-length: '. FileSize (' Example.zip ')); Set 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.