Home  >  Article  >  Backend Development  >  The role of php function header

The role of php function header

2019-08-27 13:48:5887030browse

The role of php function header

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 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 part in HTML. Likewise, body content is not a64997a0904a094b4570728d7f327acda0b5ad22ce41154d0eeb328c7ad40487. 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"');
// 打开文件,并输出

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!

The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn