php header是什么意思-php教程

资源魔 39 0

header函数正在PHP中是发送一些头部信息的, 咱们能够间接应用它来做301跳转等,上面我来总结对于header函数用法与一些罕用见成绩处理办法。

发送一个原始 HTTP 标头[Http Header]到客户端。标头 (header) 是效劳器以 HTTP 协义传 HTML 材料到阅读器前所送出的字串,正在标头与 HTML 文件之间尚需空一行分隔.

1.重定向.

Header("Location: http://www.mobiletrain.org");

exit; //正在每一个重定向之后都必需加之“exit”,防止发作谬误后,持续执行。

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.完成文件下载

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: 404 Not Found”);

然而我发事实际上阅读器前往的呼应倒是:

// ok
header(‘HTTP/1.1 200 OK’);
//设置一个404头:
header(‘HTTP/1.1 404 Not Found’);
//设置地点被永世的重定向
header(‘HTTP/1.1 301 Moved Permanently’);
HTTP/1.x 200 OK
Date: Thu, 03 Aug 2006 07:49:11 GMT
Server: Apache/2.0.55 (Win32)php/5.0.5
X-Powered-By: PHP/5.0.5
Status: 404 Not Found
Content-Length: 0
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: text/html
留意事项有如下几点.

Location以及”:”之间不克不及有空格,不然会呈现谬误(正文:我刚测试了,正在我内陆环境下,不跳转页面,然而也不报错,没有分明甚么缘由);

正在用header前不克不及有任何的输入(正文:这点各人都晓得的,假如header以前有任何的输入,包罗空缺,就会呈现header already sent by xxx的谬误);

header 前面的货色还会执行的。

保举:《PHP教程》

以上就是php header是甚么意义的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 header

抱歉,评论功能暂时关闭!