header函数的用法

header — 发送原生 HTTP 头

语法:void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

注意内容:header() 必须在任何实际输出之前调用,不管是普通的 HTML 标签,还是文件或 PHP 输出的空行,空格

几种常见的用法:

header('Location:'.$url);//跳转页面

header('content-type:text/html;charset=utf-8');//声明content-type

header('Refresh: 10; url=http://www.baidu.com/');//10s后跳转页面

 

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-cache, must-revalidate");

header("Pragma: no-cache");

 此处提供示例代码:

function http_10_cache_headers($lifeTime=null){

$gmtime=time();

if($lifeTime){

            header("Pragma: no-cache");

    }else{

            $gmtime+=$lifeTime;

             $gmtime=gmdate('D,d,M Y H:i:S',$gmtime).'GMT';

              header("Last-Modified:$gmtime");

              header("Expires:$gmtime");

     }

}

header('HTTP/1.1 401 Unauthorized');//执行http验证

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

//执行下载操作

header('Content-Type: application/octet-stream'); //设置内容类型

header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件

header('Content-Transfer-Encoding: binary'); //设置传输方式

header('Content-Length: '.filesize('example.zip')); //设置内容长度

posted @ 2017-09-19 09:54  ZhiRuSi  阅读(316)  评论(0编辑  收藏  举报