PHP header的几种用法
PHP header的几种用法
定义:header() 函数向客户端发送原始的 HTTP 报头。
1. 跳转页面
header('Location:'.$url); //Location和":"之间无空格。
2. 声明content-type
header('content-type:text/html;charset=utf-8');
3. 返回response状态码
header('HTTP/1.1 404 Not Found');
// ok
header('HTTP/1.1 200 OK');//出现错误可强制返回错误信息
4. 在某个时间后执行跳转
header('Refresh: 10; url=http://www.baidu.com/'); //10s后跳转。
5. 控制浏览器缓存
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-cache, must-revalidate"); header("Pragma: no-cache");
6. 执行http验证
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
7. 执行下载操作
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')); //设置内容长度
更多实例
<?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的几种用法的更多相关文章
- PHP header 的7种用法
这篇文章介绍的内容是关于PHP header()的7种用法 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 PHP header 的7种用法 1. 跳转页面 header('Locat ...
- PHP header 的几种用法
跳转页面 header('Location:'.$url); //Location和":"之间无空格. 声明content-type header('content-type:te ...
- using 的三种用法
using 有哪三种用法? 1)引入命名空间. 2)给命名空间或者类型起别名. 3)划定作用域.自动释放资源,使用该方法的类型必须实现了 System.IDisposable接口,当对象脱离作用域之后 ...
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...
- Wix 安装部署教程(十五) --CustomAction的七种用法
在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...
- Android Intent的几种用法全面总结
Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...
- Js闭包常见三种用法
Js闭包特性源于内部函数可以将外部函数的活动对象保存在自己的作用域链上,所以使内部函数的可以将外部函数的活动对象占为己有,可以在外部函数销毁时依然存有外部函数内的活动对象内容,这样做的好处是可 ...
- operator 的两种用法
C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).1.操作符 ...
- Service的两种用法及其生命周期
先来一点基础知识: Service 是android的四大组件之一,与Activity同属于一个级别,它是运行在后台进行服务的组件(例如在后台播放的音乐,播放音乐的同时并不影响其他操作).Servic ...
- C#中this的 四种 用法
C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码. this用法1:限定被相似的名称隐藏的成员 /// < ...
随机推荐
- excel常用函数-countif与countifs
countif用于一个条件的计数 countifs用于多个条件的计数,用法比较简单,如下: 注多条件计数的说明 :=COUNTIFS(条件匹配查询区域1,条件1,条件匹配查询区域2,条件2,以此类推. ...
- 每天学五分钟 Liunx 001 | 用户及用户组
Liunx 文件权限 [root@controller-0 ~]# ll -al heihei -rw-r--r--. 1 root root 0 Mar 3 07:39 heihei 第一列 -rw ...
- 万字血书Vue-Vue进阶
Vue进阶 生命周期 组件运行的过程 组件的生命周期是:组件从创建->运行(渲染)->销毁的整个过程,是一个时间段 如何监听组件的不同时刻 vue框架为组件内置了不同时刻的生命周期函数,是 ...
- Angular系列教程之组件
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- Laravel - except() 函数
/** * 用户添加 * @param 接收的表单数据 (name,password,id) * @return 返回添加是否成功 */ ...
- [转帖]从Linux源码看TIME_WAIT状态的持续时间
https://zhuanlan.zhihu.com/p/286537295 从Linux源码看TIME_WAIT状态的持续时间 前言 笔者一直以为在Linux下TIME_WAIT状态的Socket持 ...
- [转帖]Linux ps -o 查看进程启动时间
https://www.cnblogs.com/apink/p/17572435.html 时间参数 如下表 参数 含义 start 显示进程启动时间的简短格式.通常,它会显示日期时间中的月-日 或 ...
- [转帖]Nacos使用2.0.1版本启动出现9848端口错误的解决方式(亲测有效)
目录 一.背景 二.报错如下 三.报错原因 四.解决方式 一.背景 nacos服务端和客户端都是 2.x版本. centos7使用原始安装nacos单机版,没有使用docker安装naocs集群. 二 ...
- [转帖]UNIX SOCKET简介
UNIX Domain SOCKET 是在Socket架构上发展起来的用于同一台主机的进程间通讯(IPC).它不需要经过网络协议栈,不需要打包拆包.计算校验和.维护序列号应答等.只是将应用层数据从一个 ...
- [转帖]kafka_export 部署实战
https://zhuanlan.zhihu.com/p/57704357 Kafka Exporter 监控 Kafka 实时数据 需要安装的组件 Prometheus:时序数据库,按时间保存监控历 ...