PHPheader()函数用法
[codes=php]
/***Function:PHPheader()examples(PHP)
**Desc:Someexamplesonhowtousetheheader()functionofPHPYoufindadetailedtutorialatexpertsrt.com(English)oratffm.junetz.de(German).Theseisalsoagoodhelpaboutcachingatweb-caching.com.
**Example:seebelow.<br/><br/><b>Tip:</b>Youcanusethesesitestocheckyourheaders:<ahref="http://web-sniffer.net/">web-sniffer.net</a>,<ahref="http://www.delorie.com/web/headers.html">delorie.com</a>or<ahref="http://www.forret.com/projects/analyze/">www.forret.com</a>.
**Author:JonasJohn
*/
//fix404pages:
header('HTTP/1.1200OK');
//set404header:
header('HTTP/1.1404NotFound');
//setMovedPermanentlyheader(goodforredrictions)
//usewithlocationheader
header('HTTP/1.1301MovedPermanently');
//redirecttoanewlocation:
header('Location:http://www.example.org/');
//redrictwithdelay:
header('Refresh:10;url=http://www.example.org/');
print'Youwillberedirectedin10seconds';
//youcouldalsousetheHTMLsyntax://<metahttp-equiv="refresh"content="10;http://www.example.org//>
//overrideX-Powered-By:PHP:
header('X-Powered-By:PHP/4.4.0');
header('X-Powered-By:Brain/0.6b');
//contentlanguage(en=English)
header('Content-language:en');
//lastmodified(goodforcaching)
$time=time()–60;//orfilemtime($fn),etc
header('Last-Modified:'.gmdate('D,dMYH:i:s',$time).'GMT');
//headerfortellingthebrowserthatthecontent
//didnotgetchanged
header('HTTP/1.1304NotModified');
//setcontentlength(goodforcaching):
header('Content-Length:1234');
//Headersforandownload:
header('Content-Type:application/octet-stream');
header('Content-Disposition:attachment;filename="example.zip"');
header('Content-Transfer-Encoding:binary');
//loadthefiletosend:readfile('example.zip');
//Disablecachingofthecurrentdocument:
header('Cache-Control:no-cache,no-store,max-age=0,must-revalidate');
header('Expires:Mon,26Jul199705:00:00GMT');
//Dateinthepastheader('Pragma:no-cache');
//setcontenttype:
header('Content-Type:text/html;charset=iso-8859-1');
header('Content-Type:text/html;charset=utf-8');
header('Content-Type:text/plain');
//plaintextfile
header('Content-Type:image/jpeg');
//JPGpicture
header('Content-Type:application/zip');
//ZIPfile
header('Content-Type:application/pdf');
//PDFfile
header('Content-Type:audio/mpeg');
//AudioMPEG(MP3,…)file
header('Content-Type:application/x-shockwave-flash');
//Flashanimation//showsigninbox
header('HTTP/1.1401Unauthorized');
header('WWW-Authenticate:Basicrealm="TopSecret"');
print'Textthatwillbedisplayediftheuserhitscancelor';
print'enterswronglogindata';?>[/codes]
代码来自:http://codedump.jonasjohn.de/download/headers.txt
//下面是http://www.php.net/manual/en/function.header.php
的例子用于下载文件用
functiondl_file($file){
//First,seeifthefileexists
if(!is_file($file)){die(“404Filenotfound!“);}
//Gatherreleventinfoaboutfile
$len=filesize($file);
$filename=basename($file);
$file_extension=strtolower(substr(strrchr($filename,”.”),1));
//ThiswillsettheContent-Typetotheappropriatesettingforthefile
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;
//Thefollowingareforextensionsthatshouldn’tbedownloaded(sensitivestuff,likephpfiles)
case“php”:
case“htm”:
case“html”:
case“txt”:die(“Cannotbeusedfor“.$file_extension.”files!“);break;
default:$ctype=”application/force-download”;
}
//Beginwritingheaders
header(“Pragma:public”);
header(“Expires:0″);
header(“Cache-Control:must-revalidate,post-check=0,pre-check=0″);
header(“Cache-Control:public”);
header(“Content-Description:FileTransfer”);
//Usetheswitch-generatedContent-Type
header(“Content-Type:$ctype”);
//Forcethedownload
$header=”Content-Disposition:attachment;filename=”.$filename.”;”;
header($header);
header(“Content-Transfer-Encoding:binary”);
header(“Content-Length:“.$len);
@readfile($file);
exit;
}