基于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入门教程 精简版
Dec 13 PHP
PHP中调用ASP.NET的WebService的代码
Apr 22 PHP
遍历指定目录下的所有目录和文件的php代码
Nov 27 PHP
深入php常用函数的使用汇总
Jun 08 PHP
Thinkphp模板中截取字符串函数简介
Jun 17 PHP
PHP采集类snoopy详细介绍(snoopy使用教程)
Jun 19 PHP
php构造函数的继承方法
Feb 09 PHP
Yii中创建自己的Widget实例
Jan 05 PHP
PHP函数超时处理方法
Feb 14 PHP
PHP使用文件锁解决高并发问题示例
Mar 29 PHP
PHP7数组的底层实现示例
Aug 25 PHP
imagettftext() 失效,不起作用
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
把77A收信机改造成收音机
2021/03/02 无线电
php a simple smtp class
2007/11/26 PHP
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
2014/07/23 PHP
php实现的日历程序
2015/06/18 PHP
浅谈PHP错误类型及屏蔽方法
2017/05/27 PHP
PHP实现权限管理功能示例
2017/09/22 PHP
13个绚丽的Jquery 界面设计网站推荐
2010/09/28 Javascript
jquery 读取页面load get post ajax 四种方式代码写法
2011/04/02 Javascript
jQuery中使用了document和window哪些属性和方法小结
2011/09/13 Javascript
js函数中onmousedown和onclick的区别和联系探讨
2013/05/19 Javascript
js对象内部访问this修饰的成员函数示例
2014/04/27 Javascript
JavaScript中停止执行setInterval和setTimeout事件的方法
2015/05/14 Javascript
js+html5实现页面可刷新的倒计时效果
2017/07/15 Javascript
微信小程序云开发之新手环境配置
2019/05/16 Javascript
原生JS实现贪吃蛇小游戏
2020/03/09 Javascript
使用Taro实现小程序商城的购物车功能模块的实例代码
2020/06/05 Javascript
Js on及addEventListener原理用法区别解析
2020/07/11 Javascript
node.js爬虫框架node-crawler初体验
2020/10/29 Javascript
详解Vue2的diff算法
2021/01/06 Vue.js
Django项目中用JS实现加载子页面并传值的方法
2018/05/28 Python
Python 支付整合开发包的实现
2019/01/23 Python
pycharm new project变成灰色的解决方法
2019/06/27 Python
Python中的正则表达式与JSON数据交换格式
2019/07/03 Python
调试Django时打印SQL语句的日志代码实例
2019/09/12 Python
DJANGO-URL反向解析REVERSE实例讲解
2019/10/25 Python
英国玛莎百货新西兰:Marks & Spencer New Zealand
2019/07/21 全球购物
新西兰购物网站:TheMarket NZ
2020/09/19 全球购物
PHP笔试题
2012/02/22 面试题
网络编辑职责
2014/03/01 职场文书
大一学生职业生涯规划
2014/03/11 职场文书
水污染治理工程专业求职信
2014/06/14 职场文书
幼儿园小班见习报告
2014/10/31 职场文书
安徽导游词
2015/02/12 职场文书
建党伟业观后感
2015/06/01 职场文书
军训阅兵新闻稿
2015/07/17 职场文书
学术会议开幕词
2016/03/03 职场文书