加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php 全面解析header函数的使用方法

发布时间:2020-05-25 01:52:50 所属栏目:PHP 来源:互联网
导读:php header() 函数向客户端发送原始的HTTP报头,header() 必须在页面没有任何输出之前进行调用,否则会报错,本文章向大家介绍php header函数的使用方法和技巧

header()语法

header(string,replace,http_response_code)

header()参数

参数

描述

string

必需。规定要发送的报头字符串。

replace

可选。指示该报头是否替换之前的报头,或添加第二个报头。

默认是 true(替换)。false(允许相同类型的多个报头)。

http_response_code

可选。把 HTTP 响应代码强制为指定的值。(PHP 4 以及更高版本可用)

header()应用

1、正常访问

header('HTTP/1.1 200 OK');

2、页面不存在(404页面)

header('HTTP/1.1 404 Not Found');

header("status: 404 Not Found");

?>

3、重定向(状态码302)

header(”Location: http://www.manongjc.com”);

exit;

?>

4、永久重定向(状态码301)

Header( "HTTP/1.1 301 Moved Permanently" ) ;

Header( "Location: www.manongjc.com" );

?>

5、延迟转向,也就是隔几秒跳转

header('Refresh: 10; url=http://www.manongjc.com/');

6、修改 X-Powered-By信息

header('X-Powered-By: PHP/6.0.0');

7、设置文档语言

header('Content-language: en');

8、设置内容长度

header('Content-Length: 1234');

9、向客户端浏览器发送文件最后一次修改时间

header('Last-Modified: '.gmdate('D,d M Y H:i:s',$time).' GMT');

10、告诉浏览器文档内容没有发生改变

header('HTTP/1.1 304 Not Modified');

11、设置文件类型

header('Content-Type: text/html; charset=utf-8'); //网页编码

header('Content-Type: text/plain'); //纯文本格式

header('Content-Type: image/jpeg'); //JPG、JPEG

header('Content-Type: application/zip'); // ZIP文件

header('Content-Type: application/pdf'); // PDF文件

header('Content-Type: audio/mpeg'); // 音频文件

header('Content-type: text/css'); //css文件

header('Content-type: text/javascript'); //js文件

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

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

header('Content-type: text/xml'); //xml

header('Content-Type: application/x-shockw**e-flash'); //Flash动画

?>

12、声明一个下载的文件

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename="ITblog.zip"');

header('Content-Transfer-Encoding: binary');

readfile('test.zip');

?>

13、对当前文档禁用缓存

header('Cache-Control: no-cache,no-store,max-age=0,must-revalidate');

header('Expires: Mon,26 Jul 1997 05:00:00 GMT');

?>

14、显示一个需要验证的登陆对话框

header('HTTP/1.1 401 Unauthorized');

header('WWW-Authenticate: Basic realm="Top Secret"');

?>

以上几乎是phpheader函数在开发中会遇到的设置实例。希望对大家有所帮助。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读