PHP header 写法大全集

作者: zms! 日期: 2018.04.26 本文发布于925天前 分类: PHP函数语法点滴 相关:

PHP header 写法的跳转页面、控制浏览器缓存、执行下载操作

PHP header 的几种用法,你知道几种?


1. 跳转页面

header('Location:'.$url);  //Location和":"之间无空格。


2. 声明content-type

header('content-type:text/html;charset=utf-8');


3. 返回response状态码

header('HTTP/1.1 404 Not Found');


4. 在某个时间后执行跳转

header('Refresh: 10; url=http://www.baidu.com/');  //10s后跳转。


5. 控制浏览器缓存

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");

header("Pragma: no-cache");


6. 执行http验证

header('HTTP/1.1 401 Unauthorized');

header('WWW-Authenticate: Basic realm="Top Secret"');


7. 执行下载操作

header('Content-Type: application/octet-stream'); //设置内容类型
header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件
header('Content-Transfer-Encoding: binary'); //设置传输方式
header('Content-Length: '.filesize('example.zip')); //设置内容长度


8.Atom
header('Content-type: application/atom+xml');

9.CSS
header('Content-type: text/css');

10.Javascript
header('Content-type: text/javascript');

11.JPEG Image
header('Content-type: image/jpeg');

12.JSON
header('Content-type: application/json');  允许AJAX跨域请求: header("Access-Control-Allow-Origin: *");

13.PDF
header('Content-type: application/pdf');

14.RSS
header('Content-Type: application/rss+xml; charset=ISO-8859-1');

15.Text (Plain)
header('Content-type: text/plain');

16.XML
header('Content-type: text/xml');

17. ok
header('HTTP/1.1 200 OK');

18.设置一个404头:
header('HTTP/1.1 404 Not Found');

19.设置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');

20.转到一个新地址
header('Location: http://www.jbxue.com/');

21.文件延迟转向:
header('Refresh: 10; url=http://www.jbxue.com/');
print 'You will be redirected in 10 seconds';//当然,也可以使用html语法实现// <meta http-equiv="refresh" content="10;http://www.jbxue.com/ />

22. override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');

23.文档语言
header('Content-language: en');

24.告诉浏览器最后一次修改时间
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

25.告诉浏览器文档内容没有发生改变
header('HTTP/1.1 304 Not Modified');

26.设置内容长度
header('Content-Length: 1234');

27.设置为一个下载类型
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');

28. 对当前文档禁用缓存
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');

29.设置内容类型:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');header('Content-Type: text/plain'); //纯文本格式header('Content-Type: image/jpeg'); //JPG***header('Content-Type: application/zip'); // ZIP文件header('Content-Type: application/pdf'); // PDF文件header('Content-Type: audio/mpeg'); // 音频文件header('Content-Type: application/x-shockw**e-flash'); //Flash动画

30.显示登陆对话框
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';

未完待续。。。


返回上一页


讨论区

您尚未 登录,或请 注册

登 录 注 册


你可能也喜欢

dbMySQL数据源访问类 2020.03.14,31 pv
php计算当前时间到以后一天、一周、一月、一年 2020.01.12,29 pv
php防止CC攻击代码 php防止网页频繁刷新 2019.06.12,23 pv

php将手机号转为字符串并求所有数字之和,或求字符串中所有数字之和的两个方法: 2018.10.15,40 pv
PHP环境加固,让黑客无从下手 2018.05.15,26 pv
PHP支持手势的手机端图片裁剪上传 2018.05.09,38 pv