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的不同用法的更多相关文章

  1. PHP中header函数的用法及其注意重点是什么呢

    1.使用header函数进行跳转页面: header('Location:'.$url); 其中$url就是将要跳转的url了. 这种用法的注意事项有以下几点: •Location和":&q ...

  2. 标头 header()函数的用法

    头 (header) 是服务器以 HTTP 协议传 HTML 资料到浏览器前所送出的字串,在标头与 HTML 文件之间尚需空一行分隔. 范例一: 本例使浏览器重定向到 PHP 的官方网站. <? ...

  3. PHP header 的7种用法

    这篇文章介绍的内容是关于PHP header()的7种用法 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 PHP header 的7种用法 1. 跳转页面 header('Locat ...

  4. Cache-control使用:header('Cache-control:private')

    发布:thebaby   来源:net     [大 中 小] 转自:http://www.jbxue.com/article/5624.html网页缓存由 HTTP消息头中的“Cache-contr ...

  5. php header头信息 举例

    发布:sunday01   来源:Net     [大 中 小] 转自:http://www.jbxue.com/article/6915.html 用于演示PHP header()函数用法的代码,介 ...

  6. php header函数要点

    发布:snowfly   来源:网络     [大 中 小] 相信很多人写程序时,使用 header(location) 进行跳转往往不记得写 exit() 语句,这种做法存在严重风险. 从浏览器来看 ...

  7. PHP中的header()函数

    PHP header 函数的用法及其注意事项 void header ( string $string [, bool $replace = true [, int $http_response_co ...

  8. php 文件头部(header)

    发布:sunday01   来源:net     [大 中 小] 有关php文件头部信息(header)的详细介绍,是脚本学堂见过的最详细的一篇,有需要的朋友,千万不要错过这么好的文章. php文件头 ...

  9. 30分钟学会XAML

    1.狂妄的WPF 相对传统的Windows图形编程,需要做很多复杂的工作,引用许多不同的API.例如:WinForm(带控件表单).GDI+(2D图形).DirectX API(3D图形)以及流媒体和 ...

随机推荐

  1. vue 绑定属性(index)

    <el-menu-item v-for="item in links" :key="item.id" v-bind:index="item.id ...

  2. 软件包管理(rpm&yum)

    一.rpm包管理器 rpm是一个功能强大的包管理工具,可用于构建,安装,查询,验证,更新和卸载软件包. 用法: rpm [OPTION...] 配置文件: /var/lib/rpm/ 已安装rpm包的 ...

  3. 因为.patch_storage目录丢失,导致opatch打补丁失败

    一套新装的ORACLE Restart环境(11.2.0.3.0),计划最新的PSU,在使用opath auto方式安装补丁时报错,表面上的错误信息提示opatch工具不满足版本要求: [root@d ...

  4. 移动Web开发规范概述

    以下规范建议,均是Alloyteam在日常开发过程中总结提炼出的经验,规范具备较好的项目实践,强烈推荐使用. 字体设置 使用无衬线字体 body { font-family: "Helvet ...

  5. Unity 坐标

    一. transform.position 世界坐标 transform.localposition 相对于父亲的坐标 二. 三.应用 1.物体跟随鼠标 Vector3 screenPos = Cam ...

  6. 用servlet获取IP等信息

    Locale languageType=request.getLocale();//获取用户语言 String localIp=request.getLocalAddr();//获取本地ip int  ...

  7. 非局部均值去噪(NL-means)

    非局部均值(NL-means)是近年来提出的一项新型的去噪技术.该方法充分利用了图像中的冗余信息,在去噪的同时能最大程度地保持图像的细节特征.基本思想是:当前像素的估计值由图像中与它具有相似邻域结构的 ...

  8. fiddle

    web开发中Chrome.IE.firefox等浏览器都自带提供了插件帮助开发者跟踪http数据,在手机客户端怎么实现http数据抓包呢?Fiddler可以实现真机调试抓包.Fiddler支持Any ...

  9. (转)KICKSTART无人值守安装

    KICKSTART无人值守安装 导言 作为中小公司的运维,经常会遇到一些机械式的重复工作,例如:有时公司同时上线几十甚至上百台服务器,而且需要我们在短时间内完成系统安装. 常规的办法有什么? 光盘安装 ...

  10. js车牌号校验

    function cpyz(str) {        var newcarnum= str.value.toUpperCase();    var regExp = /(^[\u4E00-\u9FA ...