What is the header () function in PHP for? Common header Status

Source: Internet
Author: User
Tags readfile

What is header information?
Here is only a simple explanation, detailed look at the HTTP protocol.
In the HTTP protocol, the server-side answer (response) consists of two parts: header information (headers) and body content, where the header information is not part of the

What's the use of header information?
Header information has many functions, the most important of which are the following:

1, Jump: When the browser to accept the end of the information in the location:xxxx, it will automatically jump to XXXX to point to the URL address, this is a bit like using JS write jump. But this jumps only the browser to know, no matter in the body content has the thing, the user cannot see.

2, specify the content of the Web page: the same XML file, if the header information specified: Content-type:application/xml, the browser will be in accordance with the XML file format parsing. However, if the header information is: Content-type:text/xml, the browser will treat it as a saved text parsing. (The browser does not parse the file according to the extension)

3, Accessories: Do not know if we have no attention, and sometimes in some sites to download things, the point of download connection, the result of the browser will be this attachment as a Web page opened, inside the display is garbled, this problem is also related to the head information. Sometimes the browser is based on Content-type to determine whether to open or save, so sometimes you will judge the error (mainly the web designer forgot to write Content-type). In fact, there is another can specify the content as an attachment, need to save, this is: content-disposition:attachment; Filename= "xxxxx"

How do I write in PHP?
1, Jump:

The code is as follows Copy Code
Header ("location:http://www.example.com/");

2, the specified content:

The code is as follows Copy Code
Header (' content-type:application/pdf ');

3, Accessories:

The code is as follows Copy Code
Header (' content-type:application/pdf '); Specify content formatting
Header (' content-disposition:attachment; filename= "Downloaded.pdf"); Specify content as Attachment
ReadFile (' original.pdf '); Open the file and output

Finally, it is important to note that all header information must precede the body content, and that header information written by the header function is useless if there is any output. For example, at the beginning of the file <?php, if there are spaces or empty lines, the header function is useless (in fact, can be set: Output_buffer to solve, anyway), why so, you can look at the HTTP protocol, very simple.

The code is as follows Copy Code
<?php
200 normal state
Header (' http/1.1 OK ');

301 permanent Redirect, remember to add a redirect to the following address Location: $url
Header (' http/1.1 moved Permanently ');

Redirect, in fact, is 302 temporarily redirected
Header (' location:http://www.111cn.net/');

Set page 304 without modification
Header (' http/1.1 304 not Modified ');

Display the Login box,
Header (' http/1.1 401 Unauthorized ');
Header (' Www-authenticate:basic realm= ' login information ');
Echo ' Displays the information! ';

403 No Access
Header (' http/1.1 403 Forbidden ');

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

500 Server Error
Header (' http/1.1 Internal Server Error ');

REDIRECT the specified address after 3 seconds (that is, refreshing to the new page is the same as <meta http-equiv= "Refresh content=" 10;http://www.111cn.net//>)
Header (' Refresh:3; url=http://www.111cn.net/');
Echo ' 10 jump to http://www.111cn.net ';

overriding x-powered-by values
Header (' x-powered-by:php/5.3.0 ');
Header (' x-powered-by:brain/0.6b ');

Setting the context language
Header (' content-language:en ');

Set the last time the page was modified (more for anti-caching)
$time = time ()-60; It is recommended that you use the FILETIME function to set page cache time
Header (' last-modified: ' Gmdate (' d, D M Y h:i:s ', $time). ' GMT ');

Set Content length
Header (' content-length:39344 ');

Sets the header file type, which can be used for streaming files or file downloads
Header (' Content-type:application/octet-stream ');
Header (' content-disposition:attachment; filename= "Example.zip");
Header (' content-transfer-encoding:binary ');
ReadFile (' example.zip ');//Read files to Client

disabling page Caching
Header (' Cache-control:no-cache, No-store, max-age=0, Must-revalidate ');
Header (' Expires:mon, June 1997 05:00:00 GMT ');
Header (' Pragma:no-cache ');

Set Page header information
Header (' content-type:text/html; charset=iso-8859-1 ');
Header (' content-type:text/html; Charset=utf-8 ');
Header (' Content-type:text/plain ');
Header (' Content-type:image/jpeg ');
Header (' Content-type:application/zip ');
Header (' content-type:application/pdf ');
Header (' Content-type:audio/mpeg ');
Header (' Content-type:application/x-shockwave-flash ');
//.... As for the Content-type value, you can check the document library of the consortium, which is rich
?>

example, File download

  code is as follows copy code

Header (" Content-type:application/vnd.ms-excel; Charset=utf-8 ");
Header ("Pragma:public");
Header ("expires:0");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Content-type:application/force-download");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header ("Content-disposition:attachment;filename=". $title. ". XLS ");
Header ("Content-transfer-encoding:binary");

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.