A detailed _php tutorial on PHP header functions

Source: Internet
Author: User
Header function in PHP is to send some header information before, if we can directly use it to do 301 jump, and so on, let me summarize the use of the header function and some common problems to solve the problem.

Sends an original HTTP header [HTTP header] to the client. A header is a string sent by the server to the browser before the HTML data is passed through the HTTP, and a blank line is separated between the header and the HTML file.

Example 1

The code is as follows Copy Code

Header ("location:http://www.bkjia.com";);
Exit After each redirect, "Exit" must be added to avoid an error and continue execution.
?>

Prevent pages from being cached in IE

The code is as follows Copy Code

Header (' Expires:mon, 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

Implementing File Downloads

The code is as follows Copy Code

Header (' Content-type:application/octet-stream ');//Set Content type
Header (' content-disposition:attachment; filename= ' Example.zip '); Set MIME user as attachment download if you change attachment to inline meaning open online
Header (' content-transfer-encoding:binary ');//Set transfer 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

PHP's function header () can send the status header to the browser ,
Such as

The code is as follows Copy Code
Header ("status:404 not Found").

But I found that the response actually returned by the browser was:

The code is as follows Copy Code

Ok
Header (' http/1.1 OK ');

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

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


http/1.x OK
Date:thu, 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

The following points are noted:

location and ":" There can be no space between, otherwise there will be an error (note: I just tested, in my local environment, no jump page, but also no error, not clear what the reason);
• Cannot have any output before using the header (note: This is known, if the header has any output, including white space, the header already sent by xxx error);
The things behind the header will also be executed;


http://www.bkjia.com/PHPjc/445284.html www.bkjia.com true http://www.bkjia.com/PHPjc/445284.html techarticle header function in PHP is to send some header information before, if we can directly use it to do 301 jump, and so on, let me summarize the use of the header function and some common problems to solve the problem ...

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