文档章节

php header 使用详解

wankaiming
 wankaiming
发布于 2010/11/10 16:49
字数 628
阅读 292
收藏 1

PHP header()函数用法
[codes=php]
/*** Function: PHP header() examples (PHP)
** Desc: Some examples on how to use the header() function of PHPYou find a detailed tutorial at expertsrt.com (English) or at ffm.junetz.de (German).These is also a good help about caching at web-caching.com.
** Example: see below. <br/><br/><b>Tip:</b> You can use these sites to check your headers: <a href="http://web-sniffer.net/">web-sniffer.net</a>, <a href="http://www.delorie.com/web/headers.html">delorie.com</a> or <a href="http://www.forret.com/projects/analyze/">www.forret.com</a>.
** Author: Jonas John
*/

// fix 404 pages:
header('HTTP/1.1 200 OK');

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

// set Moved Permanently header (good for redrictions)
// use with location header
header('HTTP/1.1 301 Moved Permanently');

// 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';

// you could also use the HTML syntax:// <meta http-equiv="refresh" content="10;http://www.example.org/ />

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

// 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');

// 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';?>[/codes]

代码来自:http://codedump.jonasjohn.de/download/headers.txt
//下面是http://www.php.net/manual/en/function.header.php
的例子用于下载文件用
function dl_file($file){

//First, see if the file exists
if (!is_file($file)) { die(“404 File not found!“); }

//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,”.”),1));

//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
case “pdf”: $ctype=”application/pdf”; break;
case “exe”: $ctype=”application/octet-stream”; break;
case “zip”: $ctype=”application/zip”; break;
case “doc”: $ctype=”application/msword”; break;
case “xls”: $ctype=”application/vnd.ms-excel”; break;
case “ppt”: $ctype=”application/vnd.ms-powerpoint”; break;
case “gif”: $ctype=”image/gif”; break;
case “png”: $ctype=”image/png”; break;
case “jpeg”:
case “jpg”: $ctype=”image/jpg”; break;
case “mp3″: $ctype=”audio/mpeg”; break;
case “wav”: $ctype=”audio/x-wav”; break;
case “mpeg”:
case “mpg”:
case “mpe”: $ctype=”video/mpeg”; break;
case “mov”: $ctype=”video/quicktime”; break;
case “avi”: $ctype=”video/x-msvideo”; break;

//The following are for extensions that shouldn’t be downloaded (sensitive stuff, like php files)
case “php”:
case “htm”:
case “html”:
case “txt”: die(“Cannot be used for “. $file_extension .” files!“); break;

default: $ctype=”application/force-download”;
}

//Begin writing headers
header(“Pragma: public”);
header(“Expires: 0″);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(“Cache-Control: public”);
header(“Content-Description: File Transfer”);

//Use the switch-generated Content-Type
header(“Content-Type: $ctype”);

//Force the download
$header=”Content-Disposition: attachment; filename=”.$filename.”;”;
header($header );
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “.$len);
@readfile($file);
exit;
}

 

本文转载自:http://blog.csdn.net/wangschang/archive/2010/06/27/5697417.aspx

共有 人打赏支持
wankaiming

wankaiming

粉丝 72
博文 226
码字总数 60574
作品 0
广州
高级程序员
私信 提问
nginx的反向代理模块 参数proxy_pass,proxy_method,proxy_hide_

nginx的反向代理模块有很多种配置,下面介绍一些常用的配置实例: 1.proxy_pass 语法:proxy_pass URL 配置块:location,if 详解:此配置项将当前请求反向代理到URL参数指定的服务器上,URL...

adbug
2016/02/24
861
0
PHP 页面编码声明方法详解(header或meta)

php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8"); php页面为gbk编码 header("Content-type: text/html; charset=gb231......

Amamatthew
2014/09/17
0
0
在PHP中模拟post提交方式,调用JSON接口_php调用json接口

分享经验,是为了让你少走弯路。————华伟君原创·技术博客*** 在PHP中模拟post提交方式,调用JSON接口 *** 在Jquery中我们可以很方便的使用$.ajax()方法来调用数据接口,获取数据,然后进...

尘世如潮
2016/06/16
250
0
【PHP】PHP 页面编码声明方法详解(header或meta)

php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8"); php页面为gbk编码 header("Content-type: text/html; charset=gb231......

鴿神丶
2016/10/24
2
0
Nginx学习之九:实践学习指南

Nginx基础 1. nginx安装 2. nginx 编译参数详解 3. nginx安装配置+清缓存模块安装 4. nginx+PHP 5.5 5. nginx配置虚拟主机 6. nginx location配置 7. nginx root&alias文件路径配置 8. ngxht...

boonya
2014/07/09
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Linux虚拟机安装VmwareTool

1.点击虚拟机: 2.点击安装Vmware-Tools,出现如下界面,将圈起来的拖动到桌面 3.接着打开终端,执行解压缩命令 tar -xzv -f VMwareTools-10.2.5-8068393.tar.gz 这个后面的VMwareTools-10.2....

陈刚生
16分钟前
0
0
Java学习笔记(1) --JSE 、JEE、 JME三者的区别

java SE : java standard edition , JSE。它允许允许开发和部署在桌面、服务器、嵌入式环境和实时环境中使用的java应用程序。 java SE包含了java Web 服务开发的类,并为java platform ,e...

hellation_
19分钟前
1
0
kafka 环境配置

https://www.cnblogs.com/fly-piglet/p/7762556.html

steel7c4
22分钟前
0
0
关于人工智能的思考

像人类一样可以思考,可以创造的人工智能会出现吗? 个人认为:会出现。但是出现的时间,取决了于硬件架构,和运算架构实现的时候。原因在于,人类智能可以出现,机器智能为什么不可以呢,不...

janl
24分钟前
0
0
破界!Omi生态omi-mp发布,用小程序开发生成Web

omi-mp 是什么 Omi 框架是微信支付线研发部和 AlloyTeam 开源的通用 Web 组件化框架,基于 Web Components,用来开发 PC、手机浏览器或者微信、手Q webview 的 Web 页面。自今年5月开源以来,...

腾讯开源
28分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部