基于header的一些常用指令详解


Posted in PHP onJune 06, 2013

header常用指令
header分为三部分:
第一部分为HTTP协议的版本(HTTP-Version);
第二部分为状态代码(Status);
第三部分为原因短语(Reason-Phrase)。

// fix 404 pages:   用这个header指令来解决URL重写产生的404 header
header('HTTP/1.1 200 OK');  

// set 404 header:   页面没找到
header('HTTP/1.1 404 Not Found');  

//页面被永久删除,可以告诉搜索引擎更新它们的urls
// set Moved Permanently header (good for redrictions)  
// use with location header  
header('HTTP/1.1 301 Moved Permanently'); 

// 访问受限
header('HTTP/1.1 403 Forbidden');

// 服务器错误
header('HTTP/1.1 500 Internal Server Error');

// 重定向到一个新的位置
// redirect to a new location:  
header('Location: http://www.example.org/');  

延迟一段时间后重定向
// redrict with delay:  
header('Refresh: 10; url=http://www.example.org/');  
print 'You will be redirected in 10 seconds';  

// 覆盖 X-Powered-By value
// override X-Powered-By: PHP:  
header('X-Powered-By: PHP/4.4.0');  
header('X-Powered-By: Brain/0.6b');  

// 内容语言 (en = English)
// content language (en = English)  
header('Content-language: en');  

//最后修改时间(在缓存的时候可以用到)
// last modified (good for caching)  
$time = time() - 60; // or filemtime($fn), etc  
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');  

// 告诉浏览器要获取的内容还没有更新
// header for telling the browser that the content  
// did not get changed  
header('HTTP/1.1 304 Not Modified');  

// 设置内容的长度 (缓存的时候可以用到):
// set content length (good for caching):  
header('Content-Length: 1234');  

// 用来下载文件:
// Headers for an download:  
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');  
// Disable caching of the current document:  
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 pastheader('Pragma: no-cache');  
// set content type:  
header('Content-Type: text/html; charset=iso-8859-1');  
header('Content-Type: text/html; charset=utf-8');  
header('Content-Type: text/plain');  

// plain text file  
header('Content-Type: image/jpeg');  

// JPG picture  
header('Content-Type: application/zip');  

// ZIP file  
header('Content-Type: application/pdf');  

// PDF file  
header('Content-Type: audio/mpeg');  

// Audio MPEG (MP3,...) file  
header('Content-Type: application/x-shockwave-flash');  

// 显示登录对话框,可以用来进行HTTP认证
// Flash animation// show sign in box  
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';?>

// 发送一个200 正常响应
header("HTTP/1.1 200 OK");

// 发送一个404 找不到资源响应
header('HTTP/1.1 404 Not Found');

// 发送一个301 永久重定向
header('HTTP/1.1 301 Moved Permanently');

// 发送一个503 网站暂时不能访问
header('HTTP/1.1 503 Service Temporarily Unavailable');

// 网页重定向
header('Location: https://3water.com');

// 设置网页3秒后重定向
header('Refresh: 3; url=https://3water.com');
echo '网页将在3秒后跳转到https://3water.com';

// 设置网页编码
header('Content-Type: text/html; charset=utf-8');

// 设置网页输出一个图片流
header('Content-Type: image/jpeg');

// 设置网页输出一个pdf文档
header('Content-Type: application/pdf');

// 设置网页输出一个zip文档
header('Content-Type: application/zip');

PHP 相关文章推荐
小偷PHP+Html+缓存
Nov 25 PHP
PHP 函数执行效率的小比较
Oct 17 PHP
php中XMLHttpRequest(Ajax)不能设置自定义的Referer的解决方法
Nov 26 PHP
php smarty truncate UTF8乱码问题解决办法
Jun 13 PHP
常用的php图片处理类(水印、等比缩放、固定高宽)分享
Jun 19 PHP
php数组函数array_key_exists()小结
Dec 10 PHP
php安装dblib扩展,连接mssql的具体步骤
Mar 02 PHP
使用PHPStorm+XDebug搭建单步调试环境
Nov 19 PHP
学习thinkphp5.0验证类使用方法
Nov 16 PHP
PHP mongodb操作类定义与用法示例【适合mongodb2.x和mongodb3.x】
Jun 16 PHP
PHP操作Redis数据库常用方法示例
Aug 25 PHP
ThinkPhP+Apache+PHPstorm整合框架流程图解
Nov 23 PHP
深入php 正则表达式的学习探讨
Jun 06 #PHP
深入理解:单一入口、MVC、ORM、CURD、ActiveRecord概念
Jun 06 #PHP
PHP CodeBase:将时间显示为"刚刚""n分钟/小时前"的方法详解
Jun 06 #PHP
深入PHP empty(),isset(),is_null()的实例测试详解
Jun 06 #PHP
解析PHP多种序列化与反序列化的方法
Jun 06 #PHP
一个简洁的PHP可逆加密函数(分享)
Jun 06 #PHP
深入PHP获取随机数字和字母的方法详解
Jun 06 #PHP
You might like
php检测用户是否用手机(Mobile)访问网站的类
2014/01/09 PHP
PHP实现在数据库百万条数据中随机获取20条记录的方法
2017/04/19 PHP
getAsDataURL在Firefox7.0下无法预览本地图片的解决方法
2013/11/15 Javascript
jquery调取json数据实现省市级联的方法
2015/01/29 Javascript
使用Node.js实现HTTP 206内容分片的教程
2015/06/23 Javascript
Bootstrap中CSS的使用方法
2016/02/17 Javascript
让html元素随浏览器的大小自适应垂直居中的实现方法
2016/10/12 Javascript
JS作用域闭包、预解释和this关键字综合实例解析
2016/12/16 Javascript
javascript实现用户点击数量统计
2016/12/25 Javascript
jquery mobile移动端幻灯片滑动切换效果
2020/04/15 Javascript
jQuery实现鼠标经过显示动画边框特效
2017/03/24 jQuery
vue自定义一个v-model的实现代码
2018/06/21 Javascript
Angular事件之不同组件间传递数据的方法
2018/11/15 Javascript
深入理解Node内建模块和对象
2019/03/12 Javascript
多页vue应用的单页面打包方法(内含打包模式的应用)
2020/06/11 Javascript
Python实现建立SSH连接的方法
2015/06/03 Python
Python实现处理管道的方法
2015/06/04 Python
Python中使用urllib2模块编写爬虫的简单上手示例
2016/01/20 Python
vscode 远程调试python的方法
2017/12/01 Python
Python3实现带附件的定时发送邮件功能
2020/12/22 Python
python list元素为tuple时的排序方法
2018/04/18 Python
Python使用re模块正则提取字符串中括号内的内容示例
2018/06/01 Python
Python不使用int()函数把字符串转换为数字的方法
2018/07/09 Python
python的等深分箱实例
2019/11/22 Python
python requests模拟登陆github的实现方法
2019/12/26 Python
python中random.randint和random.randrange的区别详解
2020/09/20 Python
Mamas & Papas沙特阿拉伯:英国最受欢迎的婴儿品牌
2017/11/20 全球购物
AOP的定义以及作用
2013/09/08 面试题
大学活动策划书范文
2014/01/10 职场文书
2014年五一活动策划方案
2014/03/15 职场文书
争先创优心得体会
2014/09/12 职场文书
转让协议书范本
2014/09/13 职场文书
教师学习心得体会范文
2016/01/21 职场文书
实习报告范文
2019/07/30 职场文书
MySQL 分页查询的优化技巧
2021/05/12 MySQL
MySQL 那些常见的错误设计规范,你都知道吗
2021/07/16 MySQL