登录  /  注册

PHP header函数有什么样的作用?PHP header函数的介绍(附代码)

不言
发布: 2018-07-23 16:01:59
原创
1420人浏览过

php header函数有何作用?在php中header函数可以作为两种特别的头来发送状态码,可以来替换前面相同类型的头,也可以强制指定http响应的值;那么,下面我们就来看看具体的内容。

先看看官方文档的定义

(PHP 4, PHP 5, PHP 7)

header — 发送原生 HTTP 头

1 void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
登录后复制

参数:

string

有两种特别的头。第一种以"HTTP/"开头的 (case is not significant),将会被用来计算出将要发送的HTTP状态码。 例如在 Apache 服务器上用 PHP 脚本来处理不存在文件的请求(使用 ErrorDocument 指令), 就会希望脚本响应了正确的状态码。

1 <?php
2 header("HTTP/1.0 404 Not Found");
3 ?>
登录后复制

第二种特殊情况是"Location:"的头信息。它不仅把报文发送给浏览器,而且还将返回给浏览器一个 REDIRECT(302)的状态码,除非状态码已经事先被设置为了201或者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

  • 可选参数 replace 表明是否用后面的头替换前面相同类型的头。 默认情况下会替换。如果传入 FALSE,就可以强制使相同的头信息并存。例如:

1 <?php
2 header(&#39;WWW-Authenticate: Negotiate&#39;);
3 header(&#39;WWW-Authenticate: NTLM&#39;, false);
4 ?>
登录后复制

http_response_code

强制指定HTTP响应的值。注意,这个参数只有在报文字符串(string)不为空的情况下才有效。

header函数的常见用处有以下几点:

  1、重定向

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

  2、指定内容:

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

  3、附件:

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

    //指定内容为附件,指定下载显示的名字

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

    //打开文件,并输出

    readfile('original.pdf');

以上代码可以在浏览器产生文件对话框的效果

4、让用户获取最新的资料和数据而不是缓存

    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 设置临界时间

详细例子:

 <?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;); 
######
?>
登录后复制

 相关推荐:

php header 函数详解

PHP header()函数的使用

以上就是PHP header函数有什么样的作用?PHP header函数的介绍(附代码)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号