php header运用细节
http://www.111cn.net/phper/php-function/55872.htm
http://blog.sina.com.cn/s/blog_7298f36f01011dxv.html
header的用法
header()函數(shù)的作用是:發(fā)送一個原始 HTTP 標頭[Http Header]到客戶端。
標頭 (header) 是服務器以 HTTP 協(xié)義傳 HTML 資料到瀏覽器前所送出的字串,在標頭
與 HTML 文件之間尚需空一行分隔。有關(guān) HTTP 的詳細說明,可以參 RFC 2068 官方文件
(http://www.w3.org/Protocols/rfc2068/rfc2068)。
在 PHP 中送回 HTML 資料前,需先 傳完所有的標頭。
使用范例
范例一: 本例使瀏覽器重定向到 PHP 的官方網(wǎng)站。
<?PHP
Header("Location: http://www.php.net";);
exit; //在每個重定向之后都必須加上“exit",避免發(fā)生錯誤后,繼續(xù)執(zhí)行。
?>
<?php
header("refresh:3;url=http://axgle.za.net");
print('正在加載,請稍等...<br>三秒后自動跳轉(zhuǎn)~~~');
header重定向 就等價于替用戶在地址欄輸入url
?>
范例二:禁止頁面在IE中緩存
要使用者每次都能得到最新的資料,而不是 Proxy 或 cache 中的資料,可以使用下列的標頭
<?PHP
header( 'Expires: Mon, 26 Jul 1997 05: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
Expires是個好東東,如果服務器上的網(wǎng)頁經(jīng)常變化,就把它設置為-1,表示立即過期。如果一個網(wǎng)頁每天凌晨1點更新,可以把Expires設置為第二天的凌晨1點。
當HTTP1.1服務器指定CacheControl = no-cache時,瀏覽器就不會緩存該網(wǎng)頁。
舊式 HTTP 1.0 服務器不能使用 Cache-Control 標題。所以為了向后兼容 HTTP 1.0 服務器,IE使用Pragma:no-cache 標題對 HTTP 提供特殊支持。
如果客戶端通過安全連接 (https://) 與服務器通訊,且服務器在響應中返回 Pragma:no-cache 標題,則 Internet Explorer 不會緩存此響應。
注意:Pragma:no-cache 僅當在安全連接中使用時才防止緩存,如果在非安全頁中使用,處理方式與 Expires:-1 相同,該頁將被緩存,但被標記為立即過期。
http-equiv meta標記:
在html頁面中可以用http-equiv meta來標記指定的http消息頭部。老版本的IE可能不支持html meta標記,所以最好使用http消息頭部來禁用緩存。
范例三: 讓使用者的瀏覽器出現(xiàn)找不到檔案的信息。
網(wǎng)上很多資料這樣寫:php的函數(shù)header()可以向瀏覽器發(fā)送Status標頭,
如 header(”Status: 404 Not Found”)。
但是我發(fā)現(xiàn)實際上瀏覽器返回的響應卻是:
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標簽 就可以實現(xiàn)普通文件下載。如果為了保密文件,就不能把文件鏈接告訴別人,可以用header函數(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等語句,當后面遇到header()時,就會報出 “Warning: Cannot modify header information - headers already sent by ….”錯誤。就是說在這些函數(shù)的前面不能有任何文字、空行、回車等,而且最好在header()函數(shù)后加上exit()函數(shù)。例如下面的錯誤寫法,在兩個 php代碼段之間有一個空行:
//some code here
?>
//這里應該是一個空行
header(”http/1.1 403 Forbidden”);
exit();
?>
原因是:PHP腳本開始執(zhí)行 時,它可以同時發(fā)送http消息頭部(標題)信息和主體信息. http消息頭部(來自 header() 或 SetCookie() 函數(shù))并不會立即發(fā)送,相反,它被保存到一個列表中. 這樣就可以允許你修改標題信息,包括缺省的標題(例如 Content-Type 標題).但是,一旦腳本發(fā)送了任何非標題的輸出(例如,使用 HTML 或 print() 調(diào)用),那么PHP就必須先發(fā)送完所有的Header,然后終止 HTTP header.而后繼續(xù)發(fā)送主體數(shù)據(jù).從這時開始,任何添加或修改Header信息的試圖都是不允許的,并會發(fā)送上述的錯誤消息之一。
解決辦法:
修改php.ini打開緩存(output_buffering),或者在程序中使用緩存函數(shù)ob_start(),ob_end_flush()等。原 理是:output_buffering被啟用時,在腳本發(fā)送輸出時,PHP并不發(fā)送HTTP header。相反,它將此輸出通過管道(pipe)輸入到動態(tài)增加的緩存中(只能在PHP 4.0中使用,它具有中央化的輸出機制)。你仍然可以修改/添加header,或者設置cookie,因為header實際上并沒有發(fā)送。當全部腳本終止 時,PHP將自動發(fā)送HTTP header到瀏覽器,然后再發(fā)送輸出緩沖中的內(nèi)容。
=================================================================
PHP 手冊實例應用
1:您可以使用heder命令,強制使瀏覽器使用新鮮的內(nèi)容(無緩存) 。
也可以給網(wǎng)址增加了一個唯一的編號,使其每次都讀取新的內(nèi)容,避免緩存。
example:
<?
print "<img src='yourfile.jpg'>"; //通常讀取的是緩存文件
?>
<?
print "<img src='yourfile.jpg?".time()."'>"; //增加了唯一的編號,使瀏覽器重新請求
w//print "<img src='yourfile.jpg?".rand(100,999)."'>";
?>
2: 下面是個很好的函數(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;
}
?>
更多的實例:
<?php
// ok
header('HTTP/1.1 200 OK');
//設置一個404頭:
header('HTTP/1.1 404 Not Found');
//設置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');
//轉(zhuǎn)到一個新地址
header('Location: http://www.example.org/');
//文件延遲轉(zhuǎn)向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
//當然,也可以使用html語法實現(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');
//告訴瀏覽器最后一次修改時間
$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');
//設置內(nèi)容長度
header('Content-Length: 1234');
//設置為一個下載類型
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');
// 對當前文檔禁用緩存
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');
//設置內(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動畫
//顯示登陸對話框
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';
?>
轉(zhuǎn)自旭日達網(wǎng)絡
header() 函數(shù)向客戶端發(fā)送原始的 HTTP 報頭,主要包括有HTTP協(xié)議的版本、狀態(tài)代碼、原因短語等我們常用于跳轉(zhuǎn)頁面,狀態(tài)發(fā)送與文件下載,下面我們一起來看看。
header分為三部分:
第一部分為HTTP協(xié)議的版本(HTTP-Version);
第二部分為狀態(tài)代碼(Status);
第三部分為原因短語(Reason-Phrase)。
header()函數(shù)使用說明:?
一、作用:???
~~~~~~~~~???
?????? PHP只是以HTTP協(xié)議將HTML文檔的標頭送到瀏覽器,告訴瀏覽器具體怎么處理這個頁面,至于傳送的內(nèi)容則需要熟悉一下HTTP協(xié)議了,與PHP無關(guān)了,可參照http://www.w3.org/Protocols/rfc2616/rfc2616。???
?????? 傳統(tǒng)的標頭一定包含下面三種標頭之一,并只能出現(xiàn)一次。???
?????? Location:? xxxx:yyyy/zzzz???
?????? Content-Type:? xxxx/yyyy???
?????? Status:? nnn? xxxxxx?
二、先來了解一下HTTP協(xié)議的運作方式???
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~???
?????? HTTP協(xié)議是基于請求/響應范式的。一個客戶機與服務器建立連接后,發(fā)送一個請求給服務器,請求方式的格式為,統(tǒng)一資源標識符、協(xié)議版本號,后邊是MIME信息包括請求修飾符、客戶機信息和可能的內(nèi)容。服務器接到請求后,給予相應的響應信息,其格式為一個狀態(tài)行包括信息的協(xié)議版本號、一個成功或錯誤的代碼,后邊是MIME信息包括服務器信息、實體信息和可能的內(nèi)容。???
?????? 它分四個過程,在HTTP協(xié)議中,服務端是指提供HTTP服務的部分,客戶端是指你使用的瀏覽器或者下載工具等等。在通訊時,由客戶端發(fā)出請求連接,服務端建立連接;然后,客戶端發(fā)出HTTP請求(Request),服務端返回響應信息(Respond),由此完成一個HTTP操作。?
三、HTTP協(xié)議狀態(tài)碼表示的意思???
~~~~~~~~~~~~~~~~~~~~~~~~~~~~???
1×× 保留???
2×× 表示請求成功地接收???
3×× 為完成請求客戶需進一步細化請求???
4×× 客戶錯誤???
5×× 服務器錯誤?
例
代碼如下
復制代碼
// fix 404 pages: 用這個header指令來解決URL重寫產(chǎn)生的404 header
header(‘HTTP/1.1 200 OK’);
// set 404 header: 頁面沒找到
header(‘HTTP/1.1 404 Not Found’);
// 頁面被永久刪除,可以告訴seo/seo.html" rel="nofollow" target="_blank">搜索引擎更新它們的urls
// set Moved Permanently header (good for redrictions)
// use with location header
header(‘HTTP/1.1 301 Moved Permanently’);
// 訪問受限
header(‘HTTP/1.1 403 Forbidden’);
// 服務器錯誤
header(‘HTTP/1.1 500 Internal Server Error’);
// 重定向到一個新的位置
// redirect to a new location:
header(‘Location: http://www.m-bang.com);
延遲一段時間后重定向
// redrict with delay:
header(‘Refresh: 10; url=http://www.sina.com.cn’);
print ‘You will be redirected in 10 seconds’;
// 覆蓋 X-Powered-By value
// override X-Powered-By: PHP:
header(‘X-Powered-By: PHP/4.4.0′);
header(‘X-Powered-By: Brain/0.6b’);
// 內(nèi)容語言 (en = English)
// 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’);
// 告訴瀏覽器要獲取的內(nèi)容還沒有更新
// header for telling the browser that the content
// did not get changed
header(‘HTTP/1.1 304 Not Modified’);
// 設置內(nèi)容的長度 (緩存的時候可以用到):
// 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’);
// 設置內(nèi)容類型:
// 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’);
// 顯示登錄對話框,可以用來進行HTTP認證
// 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 da
ta’;
?>
現(xiàn)在表單的填寫,我們可以用AJAX對用戶隨時進行驗證,進行友好的提示,但是在用戶沒有留意AJAX友好提示,提交了錯誤的表單,跳回原頁,而填寫的信息卻全部丟失了。要支持頁面回跳,有以下的辦法:
1. 使用session_cache_limiter方法: session_cache_limiter(‘private,must-revalidate’);但是要值得注意的是 session_cache_limiter()方法要寫在session_start()方法之前才有用;
2.用header來設置控制緩存的方法: header(‘Cache-control:private,must-revalidate’);
頁面跳轉(zhuǎn)要注意的幾個問題總結(jié)
1、location和“:”號間不能有空格,否則會出錯。?
2、在用header前不能有任何的輸出。?
3、header后的PHP代碼還會被執(zhí)行。
轉(zhuǎn)載于:https://www.cnblogs.com/ryanlamp/p/4719892.html
總結(jié)
以上是生活随笔為你收集整理的php header运用细节的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有关Struts2a的ction直接使用
- 下一篇: [NOIP2014]联合权值