php header函数的详解

2025-04-28 17:49:57

1、1.重定向.查看源代码打印帮助Header("Location:http://blog.anepx.com");exit;//在每个重定向之后都必须加上“exit”,避免发生错误后,继续执行。

2、2.禁止页面在IE中缓存查看源代码打印帮助header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');header('Last-Modified: '.gmdate('D, d M Y H: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、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、4. 向浏碎茯钍婵览器发送Status标头header(”Status: 404 Not Found”);但是我发现实际上浏览器返回的响应却是:查看源代码打印帮助// okheader(催嗟祢汊‘HTTP/1.1 200 OK’);//设置一个404头:header(‘HTTP/1.1 404 Not Found’);//设置地址被永久的重定向header(‘HTTP/1.1 301 Moved Permanently’);HTTP/1.x 200 OKDate: Thu, 03 Aug 2006 07:49:11 GMTServer: Apache/2.0.55 (Win32) PHP/5.0.5X-Powered-By: PHP/5.0.5Status: 404 Not FoundContent-Length: 0Keep-Alive: timeout=15, max=98Connection: Keep-AliveContent-Type: text/html

5、注意事项有以下几点.Location和”:”之间不能有空格,否则会出现错误(注释:我刚测试了,在我本地环境下,没有跳转页面,但是也没有报错,不清楚什么原因);在用header前不能有任何的输出(注释:这点大家都知道的,如果header之前有任何的输出,包括空白,就会出现header already sent by xxx的错误);header 后面的东西还会执行的;

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢