当前位置: 移动技术网 > IT编程>开发语言>PHP > php之header的不同用法总结(实例讲解)

php之header的不同用法总结(实例讲解)

2017年12月07日  | 移动技术网IT编程  | 我要评论

地铁四重奏,狐狸雨简谱,沃德伦的领主

1、header()函数的作用是:发送一个原始 http 标头[http header]到客户端。

header(string,replace,http_response_code) 
/*string:必需。规定要发送的报头字符串。
 replace:可选。指示该报头是否替换之前的报头,或添加第二个报头。
 默认是 true(替换)。false(允许相同类型的多个报头)。
 http_response_code:可选。把 http 响应代码强制为指定的值。*/

注意:必须在任何实际的输出被发送之前调用 header() 函数。

2、 用法1:跳转页面

header("location:https://baidu.com"); //正常跳转
header('refresh: 3; url=https://www.baidu.com'); //3s后跳转
//在header作跳转时,避免发生错误后,代码继续执行,一般加个exit;

用法2:声明content-type(我经常拿来决解乱码)

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

用法3:返回响应状态码

header('http/1.1 403 forbidden');

用法4:执行下载操作(隐藏文件的位置)

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')); //设置内容长度

用法5:控制浏览器缓存

header( 'expires: mon, 26 jul 1997 05:00:00 gmt' ); //如果服务器上的网页经常变化,就把它设置为-1,表示立即过期
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' );

用法6:

3、更多实例

<?php
// ok
header('http/1.1 200 ok');
//设置一个404头:
header('http/1.1 404 not found');
//设置地址被永久的重定向
header('http/1.1 301 moved permanently');
//转到一个新地址
header('location: http://www.example.org/');
//文件延迟转向:
header('refresh: 10; url=http://www.example.org/');
print 'you will be redirected in 10 seconds';
//当然,也可以使用html语法实现
// <meta http-equiv="refresh" content="10;http://www.example.org/ />
// override x-powered-by: php:
header('x-powered-by: php/4.4.0');
header('x-powered-by: brain/0.6b');
//文档语言
header('content-language: en');
//告诉浏览器最后一次修改时间
$time = time() - 60; // or filemtime($fn), etc
header('last-modified: '.gmdate('d, d m y h:i:s', $time).' gmt');
//告诉浏览器文档内容没有发生改变
header('http/1.1 304 not modified');
//设置内容长度
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';
?>

以上这篇php之header的不同用法总结(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网