Phpheader function details _ PHP Tutorial

Source: Internet
Author: User
The phpheader function is described in detail. The header function sends some header information in php. if we can directly use it for 301 redirection, next I will summarize the usage of the header function and some common problems. solution: the header function sends some header information in php. if we can directly use it for 301 redirection, next I will summarize the usage of the header function and some common troubleshooting methods.

Send an original HTTP Header [Http Header] to the client. The header is the string sent by the server before the server passes HTML data to the browser using HTTP co-definition. a blank line is required between the header and the HTML file.

Example 1

The code is as follows:

Header ("Location: http://www.bkjia.com ";);
Exit; // after each redirection, you must add "exit" to avoid errors and continue execution.
?>

Disable page caching in IE

The code is as follows:

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-store, no-Cache, must-revalidate ');
Header ('cache-Control: post-check = 0, pre-check = 0', false );
Header ('pragma: no-cache'); // Compatible with http1.0 and https
?>
CacheControl = no-cache
Pragma = no-cache
Expires =-1

Implement file download

The code is as follows:

Header ('content-Type: application/octet-stream'); // you can specify the Content Type.
Header ('content-Disposition: attachment; filename = "example.zip" '); // sets the MIME user to be downloaded as an attachment. if you change attachment to inline, it means it is opened online.
Header ('content-Transfer-Encoding: binary '); // you can specify the transmission mode.
Header ('content-Length: '.filesize('example.zip '); // you can specify the Content Length.
// Load the file to send:
Readfile('example.zip '); // read the file to be downloaded

Php function header () can send the Status header to the browser,
For example

The code is as follows:
Header ("Status: 404 Not Found ").

However, I found that the actual response returned by the browser is:

The code is as follows:

// OK
Header ('http/1.1 200 OK ');

// Set a 404 header:
Header ('http/1.1 404 Not Found ');

// Set the address to be permanently redirected
Header ('http/1.1 301 Moved Permanently ');


HTTP/1.x 200 OK
Date: Thu, 03 Aug 2006 07:49:11 GMT
Server: Apache/2.0.55 (Win32) PHP/5.0.5
X-Powered-By: PHP/5.0.5
Status: 404 Not Found
Content-Length: 0
Keep-Alive: timeout = 15, max = 98
Connection: Keep-Alive
Content-Type: text/html

Note:

• 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;


...

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.