The role of the php function header
The function of the header() function in PHP is to send header information to the client .
Recommended: [PHP Tutorial]
What is header information?
Here is only a brief explanation, please read the http protocol in detail.
In the HTTP protocol, the server-side response content includes two parts: header information (header) and body content. The header information here is not the
part in HTML. Likewise, body content is not < /BODY>. The header information is invisible to the user and contains many items, including server information, date, content length, etc. The body content is the entire HTML, which is everything you can see.What is the use of header information?
Header information has many functions, the most important of which are the following:
1. Jump: When the browser receives the Location: xxxx in the header information, it will automatically Jump to the URL address pointed to by xxxx. This is somewhat similar to using js to write a jump. But this jump is only known by the browser, and users cannot see it regardless of whether there is anything in the content.
2. Specify the content of the web page: For the same XML file, if the header information specifies: Content-type: application/xml, the browser will parse it according to the XML file format. However, if the header information is: Content-type: text/xml, the browser will parse it as stored text. (Browsers do not parse files based on extensions)
3. Attachment: I don’t know if you have noticed that sometimes when you download something from some websites and click the download link, the browser opens the attachment as a web page. , all displayed are garbled characters, this problem is also related to the header information. Sometimes the browser determines whether to open or save based on Content-type, so sometimes it will make a wrong judgment (mainly because the website designer forgets to write Content-type). In fact, there is another way to specify that the content is an attachment and needs to be saved. This is: Content-Disposition: attachment; filename="xxxxx"
How to write it in PHP?
1. Jump:
header("Location: http://www.example.com/");
2. Specified content:
header('Content-type: application/pdf');
3. Attachment:
// 指定内容为附件 header('Content-Disposition: attachment; filename="downloaded.pdf"'); // 打开文件,并输出 readfile('original.pdf');
Finally, I would like to remind everyone to pay attention to something , all header information must precede the body content. If there is any output, the header information written by the header function will be useless. For example, at the beginning of the file
The above is the detailed content of The role of php function header. For more information, please follow other related articles on the PHP Chinese website!