Home >Backend Development >PHP Tutorial >What does the PHP header function do? Introduction to PHP header function (with code)

What does the PHP header function do? Introduction to PHP header function (with code)

不言
不言Original
2018-07-23 16:01:591707browse

What does the php header function do? In php, the header function can be used as two special headers to send status codes. It can replace the previous header of the same type, or it can force the value of the HTTP response to be specified; then, let's take a look at the specific content.

Let’s take a look at the definition in the official document first

(PHP 4, PHP 5, PHP 7)

header — Send native HTTP header

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

Parameters:

string

There are two special head of. 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 requests for files that do not exist (using the ErrorDocument directive), you will hope that the script responds with the correct status code.

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

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 ## in advance. #3xx.

1 <?php
2 header("Location: http://www.example.com/"); /* Redirect browser */
3 
4 /* Make sure that code below does not get executed when we redirect. */
5 exit;
6 ?>

 

replace

  • Optional parameterreplace Indicates whether to use the following The header replaces the previous header of the same type. Replaced by default. If you pass FALSE, you can force the same header information to coexist. For example:

  • 1 <?php
    2 header(&#39;WWW-Authenticate: Negotiate&#39;);
    3 header(&#39;WWW-Authenticate: NTLM&#39;, false);
    4 ?>

http_response_code

Forces 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. Specified content:

header ('Content-type: application/pdf'); : 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

Detailed example:

 <?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;); 
######
?>

Related recommendations:


Detailed explanation of php header function

Usage of PHP header() function

The above is the detailed content of What does the PHP header function do? Introduction to PHP header function (with code). 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