header简单用处

1、设置浏览器的显示编码

header("Content-type:text/html;charset=gb2312");

 2、使浏览器重定向到指定网站

 

Header("Location: http://test.36.net";);
exit();

 

 3、页面3秒后自动跳转

header("refresh:3;url=http://axgle.za.net");
print('三秒后自动跳转~~~');

 4、禁止页面在IE中缓存

要使用者每次都能得到最新的资料,而不是 Proxy 或 cache 中的资料,可以使用下列的标头

<?PHP
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
?>

CacheControl = no-cache
Pragma=no-cache
Expires = -1

Expires是个好东东,如果服务器上的网页经常变化,就把它设置为-1,表示立即过期。如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。
当HTTP1.1服务器指定CacheControl = no-cache时,浏览器就不会缓存该网页。
Cache-control: max-age=5 代表5秒钟内不会去访问服务器


旧式 HTTP 1.0 服务器不能使用 Cache-Control 标题。所以为了向后兼容 HTTP 1.0 服务器,IE使用Pragma:no-cache 标题对 HTTP 提供特殊支持。
如果客户端通过安全连接 (https://) 与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,则 Internet Explorer 不会缓存此响应。
注意:Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-1 相同,该页将被缓存,但被标记为立即过期。

http-equiv meta标记:

在html页面中可以用http-equiv meta来标记指定的http消息头部。老版本的IE可能不支持html meta标记,所以最好使用http消息头部来禁用缓存。

5、让使用者下载档案( 隐藏文件的位置 )

header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=文件名.php");
header("Content-Description: PHP3 Generated Data");
echo '这里是文件内容';

 6、下面是个很好的函数,将图片传送给浏览器显示

function PE_img_by_path($PE_imgpath = "")
{
    if (file_exists($PE_imgpath)) {
        $PE_imgarray = pathinfo($PE_imgpath);

        $iconcontent = file_get_contents($PE_imgpath);
        header("Content-type: image/" . $PE_imgarray["extension"]);
        header('Content-length: ' . strlen($iconcontent));
        echo $iconcontent;
        die(0);
    }
    return false;
}
PE_img_by_path('1.jpg');

7、其他设置

//设置内容长度
header('Content-Length: 1234');

//设置为一个下载类型
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');

// 对当前文档禁用缓存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

//设置内容类型:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); //纯文本格式
header('Content-Type: image/jpeg'); //JPG图片
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音频文件
header('Content-Type: application/x-shockwave-flash'); //Flash动画

//显示登陆对话框
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';
?>

 

信息来源:http://xurida.net

 

posted @ 2015-04-03 14:42  黑面骑士  阅读(692)  评论(0编辑  收藏  举报