探討php中header的用法詳解
更新時(shí)間:2013年06月07日 11:54:31 作者:
本篇文章是對(duì)php中header的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more information on HTTP headers.
范例一:
<?PHP
Header("Location: http://www.dbjr.com.cn";);
exit;//在每個(gè)重定向之后都必須加上“exit",避免發(fā)生錯(cuò)誤后,繼續(xù)執(zhí)行。
?>
<?php
header("refresh:2;url=http://www.dbjr.com.cn");
echo "正在加載,請(qǐng)稍等...<br>三秒后自動(dòng)跳轉(zhuǎn)至<a href="http://www.dbjr.com.cn" mce_href="http://www.dbjr.com.cn">百度</a>...";
?>
--------------------------------------------------------------------------------
范例二:禁止頁面在IE中緩存
使瀏覽者每次都能得到最新的資料,而不是 Proxy 或 cache 中的資料:
<?PHP
header( 'Expires: Fri, 4 Dec 2009 09:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' ); //兼容http1.0和https
?>
CacheControl = no-cache Pragma=no-cache Expires = -1
如果服務(wù)器上的網(wǎng)頁經(jīng)常變化,就把Expires設(shè)置為-1,表示立即過期。如果一個(gè)網(wǎng)頁每天凌晨1點(diǎn)更新,可以把Expires設(shè)置為第二天的凌晨1點(diǎn)。當(dāng)HTTP1.1服務(wù)器指定CacheControl = no-cache時(shí),瀏覽器就不會(huì)緩存該網(wǎng)頁。
舊式 HTTP 1.0 服務(wù)器不能使用 Cache-Control 標(biāo)題。所以為了向后兼容 HTTP 1.0 服務(wù)器,IE使用Pragma:no-cache 標(biāo)題對(duì) HTTP 提供特殊支持。如果客戶端通過安全連接 (https://) 與服務(wù)器通訊,且服務(wù)器在響應(yīng)中返回 Pragma:no-cache 標(biāo)題,則 Internet Explorer 不會(huì)緩存此響應(yīng)。
注意:Pragma:no-cache 僅當(dāng)在安全連接中使用時(shí)才防止緩存,如果在非安全頁中使用,處理方式與 Expires:-1 相同,該頁將被緩存,但被標(biāo)記為立即過期。
http-equiv meta標(biāo)記:
在html頁面中可以用http-equiv meta來標(biāo)記指定的http消息頭部。老版本的IE可能不支持html meta標(biāo)記,所以最好使用http消息頭部來禁用緩存。
--------------------------------------------------------------------------------
范例三: 讓使用者的瀏覽器出現(xiàn)找不到檔案的信息。
網(wǎng)上很多資料這樣寫:php的函數(shù)header()可以向?yàn)g覽器發(fā)送Status標(biāo)頭,
如 header(”Status: 404 Not Found”)。但實(shí)際上瀏覽器返回的響應(yīng)卻是:
HTTP/1.x 200 OK
Date: Thu, 03 Aug 2006 07:49:11 GMT
Server: Apache/2.0.55 (Win32) PHP/5.0.5
X-Powered-By: PHP/5.0.5
Status: 404 Not Found
Content-Length: 0
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: text/html
查了一些資料,正確的寫法是:
header(”http/1.1 404 Not Found”);
第一部分為HTTP協(xié)議的版本(HTTP-Version);第二部分為狀態(tài)代碼(Status);第三部分為原因短語(Reason-Phrase)。
--------------------------------------------------------------------------------
范例四:讓使用者下載檔案( 隱藏文件的位置 )
html標(biāo)簽 就可以實(shí)現(xiàn)普通文件下載。如果為了保密文件,就不能把文件鏈接告訴別人,可以用header函數(shù)實(shí)現(xiàn)文件下載。
<?php
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=文件名/");
header("Content-Description: PHP3 Generated Data");
?>
范例四:header函數(shù)前輸入內(nèi)容
一般來說在header函數(shù)前不能輸出html內(nèi)容,類似的還有setcookie() 和 session 函數(shù),這些函數(shù)需要在輸出流中增加消息頭部信息。如果在header()執(zhí)行之前有echo等語句,當(dāng)后面遇到header()時(shí),就會(huì)報(bào)出 “Warning: Cannot modify header information - headers already sent by ….”錯(cuò)誤。就是說在這些函數(shù)的前面不能有任何文字、空行、回車等,而且最好在header()函數(shù)后加上exit()函數(shù)。例如下面的錯(cuò)誤寫法,在兩個(gè)php代碼段之間有一個(gè)空行:
//some code here
?>
//這里應(yīng)該是一個(gè)空行
header(”http/1.1 403 Forbidden”);
exit();
?>
原因是:PHP腳本開始執(zhí)行 時(shí),它可以同時(shí)發(fā)送http消息頭部(標(biāo)題)信息和主體信息. http消息頭部(來自 header() 或 SetCookie() 函數(shù))并不會(huì)立即發(fā)送,相反,它被保存到一個(gè)列表中. 這樣就可以允許你修改標(biāo)題信息,包括缺省的標(biāo)題(例如 Content-Type 標(biāo)題).但是,一旦腳本發(fā)送了任何非標(biāo)題的輸出(例如,使用 HTML 或 print() 調(diào)用),那么PHP就必須先發(fā)送完所有的Header,然后終止 HTTP header.而后繼續(xù)發(fā)送主體數(shù)據(jù).從這時(shí)開始,任何添加或修改Header信息的試圖都是不允許的,并會(huì)發(fā)送上述的錯(cuò)誤消息之一。
解決辦法:
修改php.ini打開緩存(output_buffering),或者在程序中使用緩存函數(shù)ob_start(),ob_end_flush()等。原理是:output_buffering被啟用時(shí),在腳本發(fā)送輸出時(shí),PHP并不發(fā)送HTTP header。相反,它將此輸出通過管道(pipe)輸入到動(dòng)態(tài)增加的緩存中(只能在PHP 4.0中使用,它具有中央化的輸出機(jī)制)。你仍然可以修改/添加header,或者設(shè)置cookie,因?yàn)閔eader實(shí)際上并沒有發(fā)送。當(dāng)全部腳本終止時(shí),PHP將自動(dòng)發(fā)送HTTP header到瀏覽器,然后再發(fā)送輸出緩沖中的內(nèi)容。
=================================================================
PHP 手冊(cè)實(shí)例應(yīng)用
1:您可以使用heder命令,強(qiáng)制使瀏覽器使用新鮮的內(nèi)容(無緩存) 。
也可以給網(wǎng)址增加了一個(gè)唯一的編號(hào),使其每次都讀取新的內(nèi)容,避免緩存。
example:
<?
print "<img src="cs.jpg" mce_src="cs.jpg">"; //通常讀取的是緩存文件
?>
<?
print "<img src="cs.jpg?".time()."" mce_src="cs.jpg?".time()."">"; //增加了唯一的編號(hào),使瀏覽器重新請(qǐng)求
w//print "<img src="cs.jpg?".rand(100,999)."" mce_src="cs.jpg?".rand(100,999)."">";
?>
2: 下面是個(gè)很好的函數(shù),將圖片傳送給瀏覽器顯示。
<?php
function PE_img_by_path($PE_imgpath = "")
{
if (file_exists($PE_imgpath)) {
$PE_imgarray = pathinfo($PE_imgpath);
$iconcontent = file_get_contents($PE_imgpath);
header("Content-type: image/" . $PE_imgarray["extension"]);
header('Content-length: ' . strlen($iconcontent));
echo $iconcontent;
die(0);
}
return false;
}
?>
更多的實(shí)例:
<?php
// ok
header('HTTP/1.1 200 OK');
//設(shè)置一個(gè)404頭:
header('HTTP/1.1 404 Not Found');
//設(shè)置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');
//轉(zhuǎn)到一個(gè)新地址
header('Location: http://www.baidu.com');
//文件延遲轉(zhuǎn)向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
//當(dāng)然,也可以使用html語法實(shí)現(xiàn)
// <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');
//文檔語言
header('Content-language: en');
//告訴瀏覽器最后一次修改時(shí)間
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
//告訴瀏覽器文檔內(nèi)容沒有發(fā)生改變
header('HTTP/1.1 304 Not Modified');
//設(shè)置內(nèi)容長度
header('Content-Length: 1234');
//設(shè)置為一個(gè)下載類型
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');
// 對(duì)當(dāng)前文檔禁用緩存
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 past
header('Pragma: no-cache');
//設(shè)置內(nèi)容類型:
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-shockwave-flash'); //Flash動(dòng)畫
//顯示登陸對(duì)話框
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';
?>
范例一:
復(fù)制代碼 代碼如下:
<?PHP
Header("Location: http://www.dbjr.com.cn";);
exit;//在每個(gè)重定向之后都必須加上“exit",避免發(fā)生錯(cuò)誤后,繼續(xù)執(zhí)行。
?>
復(fù)制代碼 代碼如下:
<?php
header("refresh:2;url=http://www.dbjr.com.cn");
echo "正在加載,請(qǐng)稍等...<br>三秒后自動(dòng)跳轉(zhuǎn)至<a href="http://www.dbjr.com.cn" mce_href="http://www.dbjr.com.cn">百度</a>...";
?>
--------------------------------------------------------------------------------
范例二:禁止頁面在IE中緩存
使瀏覽者每次都能得到最新的資料,而不是 Proxy 或 cache 中的資料:
復(fù)制代碼 代碼如下:
<?PHP
header( 'Expires: Fri, 4 Dec 2009 09:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' ); //兼容http1.0和https
?>
CacheControl = no-cache Pragma=no-cache Expires = -1
如果服務(wù)器上的網(wǎng)頁經(jīng)常變化,就把Expires設(shè)置為-1,表示立即過期。如果一個(gè)網(wǎng)頁每天凌晨1點(diǎn)更新,可以把Expires設(shè)置為第二天的凌晨1點(diǎn)。當(dāng)HTTP1.1服務(wù)器指定CacheControl = no-cache時(shí),瀏覽器就不會(huì)緩存該網(wǎng)頁。
舊式 HTTP 1.0 服務(wù)器不能使用 Cache-Control 標(biāo)題。所以為了向后兼容 HTTP 1.0 服務(wù)器,IE使用Pragma:no-cache 標(biāo)題對(duì) HTTP 提供特殊支持。如果客戶端通過安全連接 (https://) 與服務(wù)器通訊,且服務(wù)器在響應(yīng)中返回 Pragma:no-cache 標(biāo)題,則 Internet Explorer 不會(huì)緩存此響應(yīng)。
注意:Pragma:no-cache 僅當(dāng)在安全連接中使用時(shí)才防止緩存,如果在非安全頁中使用,處理方式與 Expires:-1 相同,該頁將被緩存,但被標(biāo)記為立即過期。
http-equiv meta標(biāo)記:
在html頁面中可以用http-equiv meta來標(biāo)記指定的http消息頭部。老版本的IE可能不支持html meta標(biāo)記,所以最好使用http消息頭部來禁用緩存。
--------------------------------------------------------------------------------
范例三: 讓使用者的瀏覽器出現(xiàn)找不到檔案的信息。
網(wǎng)上很多資料這樣寫:php的函數(shù)header()可以向?yàn)g覽器發(fā)送Status標(biāo)頭,
如 header(”Status: 404 Not Found”)。但實(shí)際上瀏覽器返回的響應(yīng)卻是:
復(fù)制代碼 代碼如下:
HTTP/1.x 200 OK
Date: Thu, 03 Aug 2006 07:49:11 GMT
Server: Apache/2.0.55 (Win32) PHP/5.0.5
X-Powered-By: PHP/5.0.5
Status: 404 Not Found
Content-Length: 0
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: text/html
查了一些資料,正確的寫法是:
header(”http/1.1 404 Not Found”);
第一部分為HTTP協(xié)議的版本(HTTP-Version);第二部分為狀態(tài)代碼(Status);第三部分為原因短語(Reason-Phrase)。
--------------------------------------------------------------------------------
范例四:讓使用者下載檔案( 隱藏文件的位置 )
html標(biāo)簽 就可以實(shí)現(xiàn)普通文件下載。如果為了保密文件,就不能把文件鏈接告訴別人,可以用header函數(shù)實(shí)現(xiàn)文件下載。
復(fù)制代碼 代碼如下:
<?php
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=文件名/");
header("Content-Description: PHP3 Generated Data");
?>
范例四:header函數(shù)前輸入內(nèi)容
一般來說在header函數(shù)前不能輸出html內(nèi)容,類似的還有setcookie() 和 session 函數(shù),這些函數(shù)需要在輸出流中增加消息頭部信息。如果在header()執(zhí)行之前有echo等語句,當(dāng)后面遇到header()時(shí),就會(huì)報(bào)出 “Warning: Cannot modify header information - headers already sent by ….”錯(cuò)誤。就是說在這些函數(shù)的前面不能有任何文字、空行、回車等,而且最好在header()函數(shù)后加上exit()函數(shù)。例如下面的錯(cuò)誤寫法,在兩個(gè)php代碼段之間有一個(gè)空行:
復(fù)制代碼 代碼如下:
//some code here
?>
//這里應(yīng)該是一個(gè)空行
header(”http/1.1 403 Forbidden”);
exit();
?>
原因是:PHP腳本開始執(zhí)行 時(shí),它可以同時(shí)發(fā)送http消息頭部(標(biāo)題)信息和主體信息. http消息頭部(來自 header() 或 SetCookie() 函數(shù))并不會(huì)立即發(fā)送,相反,它被保存到一個(gè)列表中. 這樣就可以允許你修改標(biāo)題信息,包括缺省的標(biāo)題(例如 Content-Type 標(biāo)題).但是,一旦腳本發(fā)送了任何非標(biāo)題的輸出(例如,使用 HTML 或 print() 調(diào)用),那么PHP就必須先發(fā)送完所有的Header,然后終止 HTTP header.而后繼續(xù)發(fā)送主體數(shù)據(jù).從這時(shí)開始,任何添加或修改Header信息的試圖都是不允許的,并會(huì)發(fā)送上述的錯(cuò)誤消息之一。
解決辦法:
修改php.ini打開緩存(output_buffering),或者在程序中使用緩存函數(shù)ob_start(),ob_end_flush()等。原理是:output_buffering被啟用時(shí),在腳本發(fā)送輸出時(shí),PHP并不發(fā)送HTTP header。相反,它將此輸出通過管道(pipe)輸入到動(dòng)態(tài)增加的緩存中(只能在PHP 4.0中使用,它具有中央化的輸出機(jī)制)。你仍然可以修改/添加header,或者設(shè)置cookie,因?yàn)閔eader實(shí)際上并沒有發(fā)送。當(dāng)全部腳本終止時(shí),PHP將自動(dòng)發(fā)送HTTP header到瀏覽器,然后再發(fā)送輸出緩沖中的內(nèi)容。
=================================================================
PHP 手冊(cè)實(shí)例應(yīng)用
1:您可以使用heder命令,強(qiáng)制使瀏覽器使用新鮮的內(nèi)容(無緩存) 。
也可以給網(wǎng)址增加了一個(gè)唯一的編號(hào),使其每次都讀取新的內(nèi)容,避免緩存。
example:
復(fù)制代碼 代碼如下:
<?
print "<img src="cs.jpg" mce_src="cs.jpg">"; //通常讀取的是緩存文件
?>
<?
print "<img src="cs.jpg?".time()."" mce_src="cs.jpg?".time()."">"; //增加了唯一的編號(hào),使瀏覽器重新請(qǐng)求
w//print "<img src="cs.jpg?".rand(100,999)."" mce_src="cs.jpg?".rand(100,999)."">";
?>
2: 下面是個(gè)很好的函數(shù),將圖片傳送給瀏覽器顯示。
復(fù)制代碼 代碼如下:
<?php
function PE_img_by_path($PE_imgpath = "")
{
if (file_exists($PE_imgpath)) {
$PE_imgarray = pathinfo($PE_imgpath);
$iconcontent = file_get_contents($PE_imgpath);
header("Content-type: image/" . $PE_imgarray["extension"]);
header('Content-length: ' . strlen($iconcontent));
echo $iconcontent;
die(0);
}
return false;
}
?>
更多的實(shí)例:
復(fù)制代碼 代碼如下:
<?php
// ok
header('HTTP/1.1 200 OK');
//設(shè)置一個(gè)404頭:
header('HTTP/1.1 404 Not Found');
//設(shè)置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');
//轉(zhuǎn)到一個(gè)新地址
header('Location: http://www.baidu.com');
//文件延遲轉(zhuǎn)向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
//當(dāng)然,也可以使用html語法實(shí)現(xiàn)
// <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');
//文檔語言
header('Content-language: en');
//告訴瀏覽器最后一次修改時(shí)間
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
//告訴瀏覽器文檔內(nèi)容沒有發(fā)生改變
header('HTTP/1.1 304 Not Modified');
//設(shè)置內(nèi)容長度
header('Content-Length: 1234');
//設(shè)置為一個(gè)下載類型
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');
// 對(duì)當(dāng)前文檔禁用緩存
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 past
header('Pragma: no-cache');
//設(shè)置內(nèi)容類型:
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-shockwave-flash'); //Flash動(dòng)畫
//顯示登陸對(duì)話框
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';
?>
您可能感興趣的文章:
- PHP Header用于頁面跳轉(zhuǎn)要注意的幾個(gè)問題總結(jié)
- php用header函數(shù)實(shí)現(xiàn)301跳轉(zhuǎn)代碼實(shí)例
- 淺析php header 跳轉(zhuǎn)
- PHP利用header跳轉(zhuǎn)失效的解決方法
- php中header跳轉(zhuǎn)使用include包含解決參數(shù)丟失問題
- PHP頁面跳轉(zhuǎn)操作實(shí)例分析(header方法)
- php利用header函數(shù)實(shí)現(xiàn)文件下載時(shí)直接提示保存
- php header()函數(shù)使用說明
- PHP header()函數(shù)使用詳細(xì)(301、404等錯(cuò)誤設(shè)置)
- PHP 常用的header頭部定義匯總
- PHP Header用于頁面跳轉(zhuǎn)時(shí)的幾個(gè)注意事項(xiàng)
相關(guān)文章
PHP實(shí)現(xiàn)根據(jù)圖片色界在不同位置加水印的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)根據(jù)圖片色界在不同位置加水印的方法,涉及php使用MagickWand模塊操作圖片添加水印的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08PHP IDE PHPStorm配置支持友好Laravel代碼提示方法
這篇文章主要介紹了PHP IDE PHPStorm配置支持友好Laravel代碼提示方法,重點(diǎn)配置已經(jīng)加紅提示,需要的朋友可以參考下2015-05-05php中file_get_content 和curl以及fopen 效率分析
關(guān)于file_get_content 和curl以及fopen 的效率問題,小編比較傾向于使用curl來訪問遠(yuǎn)程url。Php有curl模塊擴(kuò)展,功能很是強(qiáng)大。沒事可以研究一下。2014-09-09深入理解PHP的遠(yuǎn)程多會(huì)話調(diào)試
這篇文章主要給大家介紹了關(guān)于PHP遠(yuǎn)程多會(huì)話調(diào)試的相關(guān)資料,文中通過示例代碼以及圖片介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09php實(shí)現(xiàn)xml與json之間的相互轉(zhuǎn)換功能實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)xml與json之間的相互轉(zhuǎn)換功能,結(jié)合實(shí)例形式分析了php實(shí)現(xiàn)xml轉(zhuǎn)json及json轉(zhuǎn)xml的相關(guān)原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值2016-07-07