本篇文章为大家分享php之header的不同用法总结(实例讲解),具有很好的参考价值,希望对大家有所帮助。
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如何获取ajax中的headers(案例)
php header()的7种用法
php获取ajax的headers方法与内容实例
以上就是php中header的不同用法(案例)的详细内容。