首頁  >  文章  >  後端開發  >  php中header函數的用法舉例詳解

php中header函數的用法舉例詳解

WBOY
WBOY原創
2016-07-25 08:59:04864瀏覽
複製程式碼
  1. Header("Location: http://bbs.it-home.org";);
  2. exit;//在每個重定向之後都要加上「exit",避免發生錯誤後,繼續執行。
  3. ?>
複製程式碼

2,

  1. header("refresh:2;url=http://bbs.it-home.org");
  2. echo "正在加載,請稍等...
    三秒後自動跳轉至程式設計師之家...";
  3. ?>
複製程式碼

例二:禁止頁面在IE中快取 讓瀏覽者每次都能得到最新的內容,而不是 Proxy 或 cache 中的資料:

  1. header( 'Expires: Fri, 4 Dec 2009 09:00:00 GMT' );
  2. header( 'Last -Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
  3. header( 'Cache-Control: no-store, no-cache, must-revalidate' );
  4. header( 'Cache-Control: post-check=0, pre-check=0', false );
  5. header( 'Pragma: no-cache' ); //相容於http1.0和https
  6. ? >
複製程式碼

CacheControl = no-cache Pragma=no-cache Expires = -1 如果伺服器上的網頁經常變化,就把Expires設定為-1,表示立即過期。如果一個網頁每天凌晨1點更新,可以把Expires設定為隔天的凌晨1點。當HTTP1.1伺服器指定CacheControl = no-cache時,瀏覽器就不會快取該網頁。

舊式 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訊息頭來停用快取。

例三: 讓使用者的瀏覽器出現找不到檔案的資訊。 網路上很多資料這樣寫:php的函數header()可以傳送Status標頭, 如 header(”Status: 404 Not Found”)。但實際上瀏覽器回傳的回應是:

  1. header(”http/1.1 404 Not Found”);
複製代碼

第一部分為HTTP協定的版本(HTTP-Version);第二部分為狀態碼(Status);第三部分為原因短語(Reason-Phrase)。

例四:讓使用者下載檔案( 隱藏檔案的位置 ) html標籤 就可以實現普通文件下載。如果為了保密文件,就不能把文件連結告訴別人,可以用header函數實作文件下載。

  1. header("Content-type: application/x-gzip");
  2. header("Content-Disposition: attachment ; filename=文件名/");
  3. header("Content-Description: PHP3 Generated Data");
  4. ?>
複製代碼

例四:header函數前輸入內容 一般來說在header函數前不能輸出html內容,類似的還有setcookie() 和 session 函數,這些函數需要在輸出流中增加訊息頭部資訊。如果在header()執行之前有echo等語句,當後面遇到header()時,就會報出 “Warning: Cannot modify header information - headers already sent by ….”錯誤。就是說在這些函數的前面不能有任何文字、空行、回車等,而且最好在header()函數後面加上exit()函數。例如下面的錯誤寫法,在兩個php程式碼段之間有一個空行:

  1. //some code here
  2. ?>
  3. //這裡應該是空行
  4. header(”http/1.1 403 Forbidden ”);
  5. exit();
複製程式碼

?>

原因是:PHP腳本開始執行時,它可以同時發送http訊息頭部(標題)訊息和主體訊息.http訊息頭部(來自header() 或SetCookie() 函數)並不會立即發送,相反,它被保存到一個列表中. 這樣就可以允許你修改標題信息,包括缺省的標題(例如Content-Type 標題).但是,一旦腳本發送了任何非標題的輸出(例如,使用HTML 或print( ) 呼叫),那麼PHP就必須先發送完所有的Header,然後終止HTTP header.而後繼續發送主體數據.從這時開始,任何添加或修改Header信息的試圖都是不允許的,並會發送上述的錯誤訊息之一。

解決方法: 修改php.ini開啟快取(output_buffering),或在程式中使用快取函數ob_start(),ob_end_flush()等。原理是:output_buffering啟用時,在腳本發送輸出時,PHP並不發送HTTP header。相反,它將此輸出透過管道(pipe)輸入到動態增加的快取中(只能在PHP 4.0中使用,它具有中央化的輸出機制)。你仍然可以修改/新增header,或設定cookie,因為header實際上並沒有發送。當全部腳本終止時,PHP將自動發送HTTP header到瀏覽器,然後再發送輸出緩衝中的內容。

介紹幾個php手冊中的有關header函數的小例子。

1,使用heder指令,強制使瀏覽器使用新鮮的內容(無快取) 。 為網址增加了一個獨特的編號,使其每次都讀取新的內容,避免快取。

  1. print "php中header函數的用法舉例詳解"; //通常讀取的是快取檔案
  2. ?>
  3. print "php中header函數的用法舉例詳解"; //增加了唯一的編號,使瀏覽器重新請求
  4. w//print "php中header函數的用法舉例詳解";
  5. ?>
複製程式碼

2,將圖片傳送給瀏覽器顯示。

  1. function PE_img_by_path($PE_imgpath = "")
  2. {
  3. if (files_exists(PE_img) {$ 🎜>$PE_imgarray = pathinfo($PE_imgpath);
  4. $iconcontent = file_get_contents($PE_imgpath);
  5. header("Content-type: image/" . $PE_imgarray["extension"]); ('Content-length: ' . strlen($iconcontent));
  6. echo $iconcontent;
  7. die(0);
  8. }
  9. return false;
  10. }
  11. ?>
  12. return false;
  13. }
?>
複製程式碼

附,一個完整而全面的有關header函數的應用範例。

  1. // ok
  2. header('HTTP/1.1 200 OK');
  3. //設定一個404頭:
  4. header('HTTP/1.1 404 Not Found');
  5. //設定位址被永久的重新導向
  6. header('HTTP/1.1 301 Moved Permanently');
  7. //前往一個新位址
  8. header('Location: http://www.baidu.com');
  9. //檔案延遲轉向:
  10. header('Refresh: 10; url=http://www. example.org/');
  11. print 'You will be redirected in 10 seconds';
  12. //當然,也可以使用html語法實作
  13. // header('Content-Transfer-Encoding: binary');
  14. // load the file to send:
  15. readfile('example.zip');
  16. / / 對目前文件停用快取
  17. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  18. header('Expires: Mon, 26 Jul 1997 05 :00:00 GMT'); // Date in the past
  19. header('Pragma: no-cache');
  20. //設定內容類型:
  21. header('Content-Type: text/html ; charset=iso-8859-1');
  22. header('Content-Type: text/html; charset=utf-8');
  23. header('Content-Type: text/plain'); / /純文字格式
  24. header('Content-Type: image/jpeg'); //JPG圖片
  25. header('Content-Type: application/zip'); // ZIP檔
  26. header(' Content-Type: application/pdf'); // PDF檔案
  27. header('Content-Type: audio/mpeg'); // 音訊檔案
  28. header('Content-Type: application/x-shockwave- flash'); //Flash動畫
  29. //顯示登陸對話框
  30. header('HTTP/1.1 401 Unauthorized');
  31. header('WWW-Authenticate: Basic realm="Top Secret"') ;
  32. print 'Text that will be displayed if the user hits cancel or ';
  33. print 'enters wrong login data';
  34. ?>
複製代碼?>
複製程式碼 🎜>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn