PHP的header()函数

作用向客户端发送原始的 HTTP 报头
– 请注意 header() 必须在任何实际输出之前调用,不管是普通的 HTML 标签,还是文件或 PHP 输出的空行,空格。

语法

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

参数:

  • string 头字符串

有两种特别的头。第一种以“HTTP/”开头的 (case is not significant),将会被用来计算出将要发送的HTTP状态码。 例如在 Apache 服务器上用 PHP 脚本来处理不存在文件的请求(使用 ErrorDocument 指令), 就会希望脚本响应了正确的状态码。

<?php
header("HTTP/1.0 404 Not Found");
?>

第二种特殊情况是“Location:”的头信息。它不仅把报文发送给浏览器,而且还将返回给浏览器一个 REDIRECT(302)的状态码,除非状态码已经事先被设置为了201或者3xx。

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>
  • replace

    可选参数 replace 表明是否用后面的头替换前面相同类型的头。 默认情况下会替换。如果传入 FALSE,就可以强制使相同的头信息并存。例如:

 <?php
header('WWW-Authenticate: Negotiate');
header('WWW-Authenticate: NTLM', false);
?>
  • http_response_code

    强制指定HTTP响应的值。注意,这个参数只有在报文字符串(string)不为空的情况下才有效。

头信息的主要作用

1、跳转:当浏览器接受到头信息中的 Location: xxxx 后,就会自动跳转到 xxxx 指向的URL地址,这点有点类似用 js 写跳转。但是这个跳转只有浏览器知道,不管体内容里有没有东西,用户都看不到。
例子:使浏览器重定向到 PHP 的官方网站

扫描二维码关注公众号,回复: 2726065 查看本文章
<?PHP
Header("Location: http://www.php.net";); 
exit;   //在每个重定向之后都必须加上“exit",避免发生错误后,继续执行。
?> 
<?php 
/** 
@title:PHP定时跳转 
@功能:等待指定的时间,然后再跳转到指定页面(代替html meta方式) 
*/ 
header("refresh:3;url=http://axgle.za.net"); 
print('正在加载,请稍等...<br>三秒后自动跳转~~~'); 
/* 
补充说明: 
若等待时间为0,则与header("location:")等效。 
*/ 
header重定向 就等价于替用户在地址栏输入url
?>   

2、指定网页的内容: 同样一个XML文件,如果头信息中指定:Content-type: application/xml 的话,浏览器会将其按照XML文件格式解析。但是,如果头信息中是:Content-type: text/xml 的话,浏览器就会将其看作纯文本解析。(浏览器不是按照扩展名解析文件的)HTTP Content-type 对照表
例子:

    header('Content-Type: text/html; charset=utf-8'); // 网页编码
    header('Content-Type: text/plain'); // 纯文本格式
    header('Content-Type: image/jpeg'); // JPG、JPEG 
    header('Content-Type: application/zip'); // ZIP文件
    header('Content-Type: application/pdf'); // PDF文件
    header('Content-Type: audio/mpeg'); // 音频文件 
    header('Content-type: text/css'); //css文件
    header('Content-type: text/javascript'); // js文件
    header('Content-type: application/json'); // json
    header('Content-type: application/pdf'); // pdf
    header('Content-type: text/xml'); // xml
    header('Content-Type: application/x-shockw**e-flash'); // Flash动画

3、控制浏览器缓存:PHP脚本总是会生成一些动态内容,而这些内容是不应该被缓存的,不管是客户端浏览器还是在服务器端和客户端浏览器之间的任何代理。我们可以像这样来强制设置浏览器和各个代理层不缓存数据。

<?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时,浏览器就不会缓存该网页。

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

4、向浏览器发送Status标头
例如:

    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'); // 告诉浏览器文档内容没有发生改变

5、声明下载的文件( 隐藏文件的位置 ):html标签就可以实现普通文件下载。如果为了保密文件,就不能把文件链接告诉别人,可以用header函数实现文件下载。
例如:

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>

6、更多的例子

<?php
    header('X-Powered-By: PHP/6.0.0'); // 修改 X-Powered-By信息
    header('Content-language: en'); // 文档语言
    header('Content-Length: 1234'); // 设置内容长度
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); // 告诉浏览器最后一次修改时间

    ###声明一个下载的文件###
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="ITblog.zip"');
    header('Content-Transfer-Encoding: binary');
    readfile('test.zip');
    ######

    ###显示一个需要验证的登陆对话框### 
    header('HTTP/1.1 401 Unauthorized'); 
    header('WWW-Authenticate: Basic realm="Top Secret"'); 
    ######


    ###声明一个需要下载的xls文件###
    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'); 

猜你喜欢

转载自blog.csdn.net/weixin_40155271/article/details/80835004
今日推荐