基于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 相关文章推荐
Win9x/ME下Apache+PHP安装配置
Oct 09 PHP
洪恩在线成语词典小偷程序php版
Apr 20 PHP
如何使用“PHP” 彩蛋进行敏感信息获取
Aug 07 PHP
PHP小技巧之JS和CSS优化工具Minify的使用方法
May 19 PHP
CI框架验证码CAPTCHA辅助函数用法实例
Nov 05 PHP
php常用的url处理函数总结
Nov 19 PHP
Yii2――使用数据库操作汇总(增删查改、事务)
Dec 19 PHP
PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别分析
Jul 03 PHP
PHP调用其他文件中的类
Apr 02 PHP
Laravel框架使用Redis的方法详解
May 30 PHP
Yii框架函数简单用法分析
Sep 09 PHP
php实现简易计算器
Aug 28 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
PHP5.0对象模型探索之抽象方法和抽象类
2006/09/05 PHP
php 操作数组(合并,拆分,追加,查找,删除等)
2012/07/20 PHP
php使用curl出现Expect:100-continue解决方法
2015/03/03 PHP
php输出指定时间以前时间格式的方法
2015/03/21 PHP
PHP生成(支持多模板)二维码海报代码
2018/04/30 PHP
laravel-admin自动生成模块,及相关基础配置方法
2019/10/08 PHP
PHP实现抽奖功能实例代码
2020/06/30 PHP
JavaScript 计算图片加载数量的代码
2011/01/01 Javascript
使用js画图之圆、弧、扇形
2015/01/12 Javascript
jquery实现的仿天猫侧导航tab切换效果
2015/08/24 Javascript
javascript入门之window对象【新手必看】
2016/11/22 Javascript
jQuery实现页面顶部下拉广告
2016/12/30 Javascript
ExtJs的Ext.Ajax.request实现waitMsg等待提示效果
2017/06/14 Javascript
Vuex利用state保存新闻数据实例
2017/06/28 Javascript
React 组件间的通信示例
2018/06/14 Javascript
新手快速上手webpack4打包工具的使用详解
2019/01/28 Javascript
layer.confirm()右边按钮实现href的例子
2019/09/27 Javascript
python实现中文分词FMM算法实例
2015/07/10 Python
pytorch 数据集图片显示方法
2018/07/26 Python
windows7 32、64位下python爬虫框架scrapy环境的搭建方法
2018/11/29 Python
python 移动图片到另外一个文件夹的实例
2019/01/10 Python
Python 循环终止语句的三种方法小结
2019/06/24 Python
Python常用模块logging——日志输出功能(示例代码)
2019/11/20 Python
python批量修改xml属性的实现方式
2020/03/05 Python
Python日志处理模块logging用法解析
2020/05/19 Python
Python3.7安装PyQt5 运行配置Pycharm的详细教程
2020/10/15 Python
python 下载m3u8视频的示例代码
2020/11/11 Python
开工庆典邀请函范文
2014/01/16 职场文书
中学生个人自我评价
2014/02/06 职场文书
新任教师自我鉴定
2014/02/24 职场文书
计算机专业职业规划
2014/02/28 职场文书
房屋委托书范本
2014/04/04 职场文书
2015年银行大堂经理工作总结
2015/04/24 职场文书
2019年朋友圈经典励志语录50条
2019/07/05 职场文书
Python Socket编程详解
2021/04/25 Python
MySQL数据库必备之条件查询语句
2021/10/15 MySQL