基于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的栏目导航程序
Oct 09 PHP
通过ODBC连接的SQL SERVER实例
Oct 09 PHP
实例(Smarty+FCKeditor新闻系统)
Jan 02 PHP
PHP 编写大型网站问题集
May 07 PHP
php excel类 phpExcel使用方法介绍
Aug 21 PHP
通过php快速统计某个数据库中每张表的数据量
Sep 04 PHP
百度工程师讲PHP函数的实现原理及性能分析(二)
May 13 PHP
PHP微信公众号自动发送红包API
Jun 01 PHP
PHP实现的大文件切割与合并功能示例
Apr 10 PHP
PHP cURL获取微信公众号access_token的实例
Apr 28 PHP
PHP7中I/O模型内核剖析详解
Apr 14 PHP
THINKPHP-Apache服务器中使用Alias虚拟目录URL重写 隐藏index.php
Mar 09 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批量生成缩略图的代码
2008/07/19 PHP
php+ajax实现无刷新动态加载数据技术
2015/04/28 PHP
thinkphp多层MVC用法分析
2015/12/30 PHP
PHP实现的简单异常处理类示例
2017/05/04 PHP
thinkphp中U方法按路由规则生成url的方法
2018/03/12 PHP
静态图片的十一种滤镜效果--不支持Ie7及非IE浏览器。
2007/03/06 Javascript
Mootools 1.2教程(21)——类(二)
2009/09/15 Javascript
JavaScript 内置对象属性及方法集合
2010/07/04 Javascript
javascript开发技术大全 第4章 直接量与字符集
2011/07/03 Javascript
人人网javascript面试题 可以提前实现下
2012/01/05 Javascript
JavaScript高级程序设计 读书笔记之十一 内置对象Global
2012/03/07 Javascript
JS获取页面input控件中所有text控件并追加样式属性
2013/02/25 Javascript
getAsDataURL在Firefox7.0下无法预览本地图片的解决方法
2013/11/15 Javascript
深入理解JavaScript系列(46):代码复用模式(推荐篇)详解
2015/03/04 Javascript
javascript字符串与数组转换汇总
2015/05/26 Javascript
Hallo.js基于jQuery UI所见即所得的Web编辑器
2016/01/26 Javascript
Vuex利用state保存新闻数据实例
2017/06/28 Javascript
jQuery列表检索功能实现代码
2017/07/17 jQuery
浅谈JavaScript作用域和闭包
2017/09/18 Javascript
微信小程序开发摇一摇功能
2019/11/22 Javascript
通过实例解析js可枚举属性与不可枚举属性
2020/12/02 Javascript
用python实现批量重命名文件的代码
2012/05/25 Python
Python利用itchat对微信中好友数据实现简单分析的方法
2017/11/21 Python
Python3读取Excel数据存入MySQL的方法
2018/05/04 Python
Flask实现跨域请求的处理方法
2018/09/27 Python
Python 使用 docopt 解析json参数文件过程讲解
2019/08/13 Python
python编写简单端口扫描器
2019/09/04 Python
pip安装tensorflow的坑的解决
2020/04/19 Python
HTML5中div、article、section的区别及使用介绍
2013/08/14 HTML / CSS
阿里巴巴Oracle DBA笔试题答案-备份恢复类
2013/11/20 面试题
董事长秘书岗位职责
2013/11/29 职场文书
餐饮服务员岗位职责
2015/02/09 职场文书
借款民事起诉状范文
2015/05/19 职场文书
php访问对象中的成员的实例方法
2021/11/17 PHP
vue打包时去掉所有的console.log
2022/04/10 Vue.js
MySQL创建管理LIST分区
2022/04/13 MySQL