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 <?php2 header("http/1.0 404 not found");3 ?>
第二种特殊情况是"location:"的头信息。它不仅把报文发送给浏览器,而且还将返回给浏览器一个 redirect(302)的状态码,除非状态码已经事先被设置为了201或者3xx。
1 <?php2 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 <?php2 header('www-authenticate: negotiate');3 header('www-authenticate: ntlm', 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"); // 设置临界时间
详细例子:
<?phpheader('http/1.1 200 ok'); // ok 正常访问header('http/1.1 404 not found'); //通知浏览器 页面不存在header('http/1.1 301 moved permanently'); //设置地址被永久的重定向 301header('location: http://www.ithhc.cn/'); //跳转到一个新的地址header('refresh: 10; url=http://www.ithhc.cn/'); //延迟转向 也就是隔几秒跳转header('x-powered-by: php/6.0.0'); //修改 x-powered-by信息header('content-language: en'); //文档语言header('content-length: 1234'); //设置内容长度header('last-modified: '.gmdate('d, d m y h:i:s', $time).' gmt'); //告诉浏览器最后一次修改时间header('http/1.1 304 not modified'); //告诉浏览器文档内容没有发生改变 ###内容类型###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'); //jsonheader('content-type: application/pdf'); //pdfheader('content-type: text/xml'); //xmlheader('content-type: application/x-shockw**e-flash'); //flash动画 ###### ###声明一个下载的文件###header('content-type: application/octet-stream');header('content-disposition: attachment; filename="itblog.zip"');header('content-transfer-encoding: binary');readfile('test.zip');###### ###对当前文档禁用缓存###header('cache-control: no-cache, no-store, max-age=0, must-revalidate');header('expires: mon, 26 jul 1997 05:00:00 gmt');###### ###显示一个需要验证的登陆对话框### header('http/1.1 401 unauthorized'); header('www-authenticate: basic realm="top secret"'); ###### ###声明一个需要下载的xls文件###header('content-disposition: attachment; filename=ithhc.xlsx');header('content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');header('content-length: '.filesize('./test.xls')); header('content-transfer-encoding: binary'); header('cache-control: must-revalidate'); header('pragma: public'); readfile('./test.xls'); ######?>
相关推荐:
php header 函数详解
php header()函数的使用
以上就是php header函数有什么样的作用?php header函数的介绍(附代码)的详细内容。