header() 函数向客户端发送原始的 HTTP 报头,认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决此问题):
- <html>
- <?php
- // 结果出错
- // 在调用 header() 之前已存在输出
- header('Location: http://www.phpfensi.com/');
- ?>
语法:
header(string,replace,http_response_code)参数 描述
string 必需,规定要发送的报头字符串。
replace 可选,指示该报头是否替换之前的报头,或添加第二个报头。
默认是 true(替换),false(允许相同类型的多个报头).
http_response_code 可选,把 HTTP 响应代码强制为指定的值(PHP 4 以及更高版本可用)
提示和注释
注释:从 PHP 4.4 之后,该函数防止一次发送多个报头,这是对头部注入攻击的保护措施.
例子1:
- <?php
- // Date in the past
- header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
- header("Cache-Control: no-cache");
- header("Pragma: no-cache");
- ?>
- <html>
- <body>
注释:用户可能会设置一些选项来更改浏览器的默认缓存设置,通过发送上面的报头,您可以覆盖任何这些设置,强制浏览器不进行缓存!
例子2
提示用户保存一个生成的 PDF 文件(Content-Disposition 报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框):
- <?php
- header("Content-type:application/pdf");
- // 文件将被称为 downloaded.pdf
- header("Content-Disposition:attachment;filename='downloaded.pdf'");
- // PDF 源在 original.pdf 中
- readfile("original.pdf");
- ?>
- <html>
- <body>
- …
- …
- <?php
- // 结果出错
- // 在调用 header() 之前已存在输出
- header('location: http://www.phpfensi.com/');
- ?>
语法:header(string,replace,http_response_code)
该函数防止一次发送多个报头:
- <?php
- // date in the past
- header("expires: mon, 26 jul 1997 05:00:00 gmt");
- header("cache-control: no-cache");
- header("pragma: no-cache");
- ?>
提示用户保存一个生成的 pdf 文件(content-disposition 报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框):当时间超过指定时间就表示网页内容已经失效。 其格式如下:
expires = “expires:” http-date
例 如: header(“expires: fri, 31 oct 2003 18:00:00 gmt”); 表示2003年10月31日星期五 18时。
header(“expires: wed, 30 jun 2004 09:00:00 gmt”); 表示2004年6月30日星期三 9时。
last-modified = “last-modified” “:” http-date
例如 header(“last-modified: wed, 01 jan 2003 12:00:00 gmt”); 上次修改时间2003年1月1日12时
cache control
no-cache 使得服务器的数据可以传送到远程的使用者,而不会被暂存(cache)起来,
no-store 是预防不良的版本或者是敏感的信息被保留下来。
must-revalidate 是指必须要再评估信息, 如果暂存 (cached) 的数据是无效的则让服务器与使用者端联机.
波比源码 » php 中header函数的用法详解
order levaquin generic levofloxacin for sale online
order ampicillin 250mg generic erythromycin 500mg sale erythromycin 500mg pills
buy lamotrigine pills generic minipress 1mg tretinoin gel us
buy isotretinoin 40mg pills brand zithromax zithromax 500mg usa
tadalafil 40mg en france pharmacie en ligne cialis 5mg sildenafil pour homme
buy isotretinoin 20mg online cheap buy isotretinoin 10mg generic ivermectin 90 mg
doxycycline medication doxycycline 100mg usa buy furosemide 40mg pills
order buspar generic buspirone without prescription oxybutynin 2.5mg pills
order alendronate 70mg pills buy ibuprofen 600mg generic pepcid 20mg pills
imuran 100 mcg generic buy sildenafil 100mg online cheap sildenafil 25 mg
usa pharmacy cialis how to order viagra online cheap viagra pill
order nootropil 800 mg online cheap buy piracetam online sildenafil 50mg uk
buy cialis without prescription cialis oral viagra 50mg for sale
purchase norvasc online cheap viagra 100mg price cialis 10mg without prescription
buy zyvox 600 mg sale poker online for fun money slots
vardenafil sale methylprednisolone 16mg over counter medrol for sale online
essays help sildenafil 100mg pills for men viagra 100mg us
oral clomid cost clomiphene 50mg gambling casino
tadalafil 20mg sale viagra 150mg purchase sildenafil
zyloprim 100mg cheap rosuvastatin 20mg cost brand ezetimibe
ozobax pills baclofen 25mg canada buy toradol
buy generic imitrex 50mg sumatriptan usa dutasteride usa
purchase colchicine online cheap colchicine that roulette
online poker best casino casino money
cialis medication ampicillin 250mg over the counter cost cipro
buy generic simvastatin 10mg finasteride 5mg cheap order finasteride 1mg sale
cephalexin 500mg us erythromycin cheap erythromycin 500mg brand
ceftin 250mg generic purchase careprost pill methocarbamol 500mg drug