php header

Posted 雪剑无影

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php header相关的知识,希望对你有一定的参考价值。

返回response状态码

header(‘HTTP/1.1 200 OK‘); // ok 正常访问
header(‘HTTP/1.1 404 Not Found‘); //通知浏览器 页面不存在
header(‘HTTP/1.1 301 Moved Permanently‘); //设置地址被永久的重定向 301
header(‘HTTP/1.1 304 Not Modified‘); //告诉浏览器文档内容没有发生改变

设置修改 X-Powered-By信息

header(‘X-Powered-By: php/6.0.0‘);

 文档语言

header(‘Content-language: en‘);

内容长度

header(‘Content-Length: 1024‘);

内容类型

header(‘Content-Type: text/html; charset=utf-8‘); //网页格式,网页编码
header(‘Content-Type: text/plain‘); //纯文本格式

设置浏览器最后一次修改时间

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

对当前文档禁用缓存

header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate‘);
header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘);
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

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

header(‘HTTP/1.1 401 Unauthorized‘); 
header(‘WWW-Authenticate: Basic realm="Top Secret"‘); 

跳转页面

header(‘Location:‘.$url);  //Location和":"之间无空格
header(‘Refresh: 10; url=http://www.baidu.com/‘);  //10s后跳转

声明一个下载的文件

// 下载 zip
header(‘Content-Type: application/octet-stream‘); //设置内容类型
header(‘Content-Disposition: attachment; filename="ITblog.zip"‘); //设置MIME用户作为附件
header(‘Content-Transfer-Encoding: binary‘); //设置传输方式
readfile(‘test.zip‘); // 打开文件,输出内容
// 下载 execl
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下载一个svg

超级有用的9个PHP代码片段

PHP必用代码片段

PHP代码-psysh调试代码片段工具

PHP实现查看邮件是否被阅读

超实用的php代码片段