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

The role of php header

angryTom
angryTomOriginal
2019-08-23 13:26:213905browse

The role of php header

This article mainly introduces you to several major functions of the PHP header function. I hope it can be helpful to you.

Recommended tutorial: PHP video tutorial

First look at the definition of the official document

( PHP 4, PHP 5, PHP 7)

header — Send native HTTP header

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

Parameters:

string

There are two special heads. The first one starting with "HTTP/" (case is not significant) will be used to calculate the HTTP status code to be sent. For example, if you use a PHP script on the Apache server to handle a request for a file that does not exist (using the ErrorDocument directive), you will hope that the script responds with the correct status code.

<?php
     header("HTTP/1.0 404 Not Found");
?>

The second special case is the "Location:" header information. It not only sends the message to the browser, but also returns a REDIRECT (302) status code to the browser, unless the status code has been set to 201 or 3xx in advance.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

replace

The optional parameter replace indicates whether to replace the previous header of the same type with the later header. Replaced by default. If FALSE is passed in, the same header information can be forced to coexist. For example:

<?php
     header(&#39;WWW-Authenticate: Negotiate&#39;);
     header(&#39;WWW-Authenticate: NTLM&#39;, false);
 ?>

http_response_code

Forcefully specify the value of the HTTP response. Note that this parameter is only valid when the message string (string) is not empty.

The common uses of the header function are as follows:

 1. Redirection

Header('Location: http://www.example.com/');

2. Specify content:

header('Content-type : application/pdf');

 3. Attachment:

  header('Content-type: application/pdf');

  // Specify the content as an attachment and specify the name for download display

Header('Content-Disposition: attachment; filename="downloaded.pdf"');

//Open the file and output

readfile('original.pdf');

The above code can produce the effect of a file dialog box in the browser

4. Allow users to obtain the latest information and data Instead of caching

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00 :00 GMT"); // Set critical time

<?php
header(&#39;HTTP/1.1 200 OK&#39;); // ok 正常访问
header(&#39;HTTP/1.1 404 Not Found&#39;); //通知浏览器 页面不存在
header(&#39;HTTP/1.1 301 Moved Permanently&#39;); //设置地址被永久的重定向 301
header(&#39;Location: http://www.ithhc.cn/&#39;); //跳转到一个新的地址
header(&#39;Refresh: 10; url=http://www.ithhc.cn/&#39;); //延迟转向 也就是隔几秒跳转
header(&#39;X-Powered-By: PHP/6.0.0&#39;); //修改 X-Powered-By信息
header(&#39;Content-language: en&#39;); //文档语言
header(&#39;Content-Length: 1234&#39;); //设置内容长度
header(&#39;Last-Modified: &#39;.gmdate(&#39;D, d M Y H:i:s&#39;, $time).&#39; GMT&#39;); //告诉浏览器最后一次修改时间
header(&#39;HTTP/1.1 304 Not Modified&#39;); //告诉浏览器文档内容没有发生改变
 
###内容类型###
header(&#39;Content-Type: text/html; charset=utf-8&#39;); //网页编码
header(&#39;Content-Type: text/plain&#39;); //纯文本格式
header(&#39;Content-Type: image/jpeg&#39;); //JPG、JPEG 
header(&#39;Content-Type: application/zip&#39;); // ZIP文件
header(&#39;Content-Type: application/pdf&#39;); // PDF文件
header(&#39;Content-Type: audio/mpeg&#39;); // 音频文件 
header(&#39;Content-type: text/css&#39;); //css文件
header(&#39;Content-type: text/javascript&#39;); //js文件
header(&#39;Content-type: application/json&#39;); //json
header(&#39;Content-type: application/pdf&#39;); //pdf
header(&#39;Content-type: text/xml&#39;); //xml
header(&#39;Content-Type: application/x-shockw**e-flash&#39;); //Flash动画
 
######
 
###声明一个下载的文件###
header(&#39;Content-Type: application/octet-stream&#39;);
header(&#39;Content-Disposition: attachment; filename="ITblog.zip"&#39;);
header(&#39;Content-Transfer-Encoding: binary&#39;);
readfile(&#39;test.zip&#39;);
######
 
###对当前文档禁用缓存###
header(&#39;Cache-Control: no-cache, no-store, max-age=0, must-revalidate&#39;);
header(&#39;Expires: Mon, 26 Jul 1997 05:00:00 GMT&#39;);
######
 
###显示一个需要验证的登陆对话框### 
header(&#39;HTTP/1.1 401 Unauthorized&#39;); 
header(&#39;WWW-Authenticate: Basic realm="Top Secret"&#39;); 
######
 
 
###声明一个需要下载的xls文件###
header(&#39;Content-Disposition: attachment; filename=ithhc.xlsx&#39;);
header(&#39;Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#39;);
header(&#39;Content-Length: &#39;.filesize(&#39;./test.xls&#39;)); 
header(&#39;Content-Transfer-Encoding: binary&#39;); 
header(&#39;Cache-Control: must-revalidate&#39;); 
header(&#39;Pragma: public&#39;); 
readfile(&#39;./test.xls&#39;); 
######
?>

The above is the detailed content of The role of php header. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to use define in phpNext article:How to use define in php