基于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 相关文章推荐
Linux下ZendOptimizer的安装与配置方法
Apr 12 PHP
Windows下利用Gvim写PHP产生中文乱码问题解决方法
Apr 20 PHP
PHP支持多种格式图片上传(支持jpg、png、gif)
Nov 03 PHP
php在程序中将网页生成word文档并提供下载的代码
Oct 09 PHP
PHP数据过滤的方法
Oct 30 PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
Apr 17 PHP
超详细的php用户注册页面填写信息完整实例(附源码)
Nov 17 PHP
php简单压缩css样式示例
Sep 22 PHP
PHP删除数组中指定下标的元素方法
Feb 03 PHP
浅谈PHP中pack、unpack的详细用法
Mar 12 PHP
php post json参数的传递和接收处理方法
May 31 PHP
PHP操作XML中XPath的应用示例
Jul 04 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下连接mssql2005的代码
2011/01/17 PHP
解析CI即CodeIgniter框架在Nginx下的重写规则
2013/06/03 PHP
5款适合PHP使用的HTML编辑器推荐
2015/07/03 PHP
php array_merge_recursive 数组合并
2016/10/26 PHP
javascript 添加和移除函数的通用方法
2009/10/20 Javascript
javascript 面向对象的JavaScript类
2010/05/04 Javascript
jQuery实现字符串按指定长度加入特定内容的方法
2015/03/11 Javascript
JavaScript中常见的字符串操作函数及用法汇总
2015/05/04 Javascript
jQuery旋转插件jqueryrotate用法详解
2016/10/13 Javascript
Vue实现typeahead组件功能(非常靠谱)
2017/08/26 Javascript
使用DataTable插件实现异步加载数据
2017/11/19 Javascript
p5.js入门教程之平滑过渡(Easing)
2018/03/16 Javascript
用ES6写全屏滚动插件的示例代码
2018/05/02 Javascript
Angular实现模版驱动表单的自定义校验功能(密码确认为例)
2018/05/17 Javascript
微信小程序日历/日期选择插件使用方法详解
2018/12/28 Javascript
[01:14:41]DOTA2-DPC中国联赛定级赛 iG vs Magma BO3第一场 1月8日
2021/03/11 DOTA
Python中使用PDB库调试程序
2015/04/05 Python
CentOS下Python3的安装及创建虚拟环境的方法
2018/11/28 Python
Python开发之Nginx+uWSGI+virtualenv多项目部署教程
2019/05/13 Python
Python字符串对象实现原理详解
2019/07/01 Python
利用Python库Scapy解析pcap文件的方法
2019/07/23 Python
python使用writerows写csv文件产生多余空行的处理方法
2019/08/01 Python
python实现上传文件到linux指定目录的方法
2020/01/03 Python
Python更新所有已安装包的操作
2020/02/13 Python
使用keras实现Precise, Recall, F1-socre方式
2020/06/15 Python
对Python 字典元素进行删除的方法
2020/07/31 Python
常用的四种CSS透明属性介绍
2014/04/12 HTML / CSS
HTML5地理定位与第三方工具百度地图的应用
2016/11/17 HTML / CSS
英国最大的婴儿监视器网上商店:Baby Monitors Direct
2018/04/24 全球购物
电台编导求职信
2014/05/06 职场文书
提拔干部考察材料
2014/05/26 职场文书
2014年个人售房协议书
2014/10/30 职场文书
义诊活动总结
2015/02/04 职场文书
主持人大赛开场白
2015/05/29 职场文书
文艺委员竞选稿
2015/11/19 职场文书
MySQL分布式恢复进阶
2022/07/23 MySQL