当前位置: 移动技术网 > IT编程>开发语言>PHP > 基于header的一些常用指令详解

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

2019年04月10日  | 移动技术网IT编程  | 我要评论

长岛政府网,emule 搜索,3699双人小游戏

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: //www.jb51.net');

// 设置网页3秒后重定向
header('refresh: 3; url=//www.jb51.net');
echo '网页将在3秒后跳转到//www.jb51.net';

// 设置网页编码
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');

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网