发送一个原始HTTP标头[]到客户端。标头()是服务器以HTTP协义传HTML资料到浏览器前所送出的字串,在标头与HTML文件之间尚需空一行分隔.
1.重定向.
();
exit;//在每个重定向之后都必须加上“exit”,避免发生错误后,继续执行。
2.禁止页面在IE中缓存
(,);
(Last-(D,dMYH:i:s).GMT);
header(Cache-Control:no-store,no-cache,must-revalidate);
header(Cache-Control:post-check=0,pre-check=0,false);
header(Pragma:no-cache);//兼容http1.0和https
3.实现文件下载
header(Content-Type:application/octet-stream//设置内容类型
header(Content-Disposition:attachment;filename=example.zip//设置MIME用户作为附件下载如果将attachment换成inline意思为在线打开
header(Content-Transfer-Encoding:binary//设置传输方式
header(Content-Length:.filesize(example.zip));//设置内容长度
readfile(example.zip//读取需要下载的文件
4.向浏览器发送Status标头
header(”Status:404NotFound”);
但是我发现实际上浏览器返回的响应却是:
//ok
header(‘HTTP/1.1200OK’);
//设置一个404头:
header(‘HTTP/1.1404NotFound’);
//设置地址被永久的重定向
header(‘HTTP/1.1301MovedPermanently’);
HTTP/1.x200OK
Date:Thu,03Aug200607:49:11GMT
Server:Apache/2.0.55(Win32)php/5.0.5
X-Powered-By:PHP/5.0.5
Status:404NotFound
Content-Length:0
Keep-Alive:timeout=15,max=98
Connection:Keep-Alive
Content-Type:text/html
注意事项有以下几点.
Location和”:”之间不能有空格,否则会出现错误(注释:我刚测试了,在我本地环境下,没有跳转页面,但是也没有报错,不清楚什么原因);
在用header前不能有任何的输出(注释:这点大家都知道的,如果header之前有任何的输出,包括空白,就会出现headeralreadysentbyxxx的错误);
header后面的东西还会执行的。
推荐:《PHP教程》