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=">,<ahref=">or<ahref=">.**Author:JonasJohn*///fix404pages:
header('HTTP/1.1200OK');//set404header:
header('HTTP/1.1404NotFound');//setMovedPermanentlyheader(goodforredrictions)
//usewithlocationheaderheader('HTTP/1.1301MovedPermanently');//redirecttoanewlocation:
header('Location:);//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),etcheader('Last-Modified:'.gmdate('D,dMYH:i:s',$time).'GMT');//headerfortellingthebrowserthatthecontent
//didnotgetchangedheader('HTTP/1.1304NotModified');//setcontentlength(goodforcaching):
header('Content-Length:1234');//Headersforandownload:
header('Content-Type:application/octet-stream');header('Content-Disposition:p_w_upload;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:p_w_picpath/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]代码来自:
//下面是的例子用于下载文件用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=”p_w_picpath/gif”;break;case“png”:$ctype=”p_w_picpath/png”;break;case“jpeg”:case“jpg”:$ctype=”p_w_picpath/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:p_w_upload;filename=”.$filename.”;”;header($header);header(“Content-Transfer-Encoding:binary”);header(“Content-Length:“.$len);@readfile($file);exit;}