php header 使用詳解

來源:互聯網
上載者:User
  1. Header("Location: http://www.php.net");
  2. ?>
複製代碼

<2> 強制使用者每次訪問這個頁面時擷取最新資料,而不是使用存在用戶端的緩衝。

  1. //告訴瀏覽器此頁面的到期時間(用格林威治時間表示),只要是已經過去的日期即可。
  2. header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
  3. //告訴瀏覽器此頁面的最後更新日期(用格林威治時間表示)也就是當天,目的就是強迫瀏覽器擷取最新資料
  4. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
  5. //告訴用戶端瀏覽器不使用緩衝
  6. header("Cache-Control: no-cache, must-revalidate");
  7. //參數(與以前的伺服器相容),即相容HTTP1.0協議
  8. header("Pragma: no-cache");
  9. //輸出MIME類型
  10. header("Content-type: application/file");
  11. //檔案長度
  12. header("Content-Length: 227685");
  13. //接受的範圍單位
  14. header("Accept-Ranges: bytes");
  15. //預設時檔案儲存對話方塊中的檔案名稱
  16. header("Content-Disposition: attachment; filename=$filename");
  17. ?>
複製代碼

<3> 輸出狀態值到瀏覽器,主要用於存取權限控制

  1. header('HTTP/1.1 401 Unauthorized');
  2. header('status: 401 Unauthorized');
  3. ?>
複製代碼

比如要限制一個使用者不能訪問該頁,則可設定狀態為404,如下所示,這樣瀏覽器就顯示為即該頁不存在

  1. header('HTTP/1.1 404 Not Found');
  2. header("status: 404 Not Found");
  3. ?>
複製代碼

注意: 傳統的標題一定包含下面三種標題之一,並只能出現一次。 Content-Type: xxxx/yyyy Location: xxxx:yyyy/zzzz Status: nnn xxxxxx 在新的多型標題規格 (Multipart MIME) 方可以出現二次以上。以上就是有關php header頭資訊的內容介紹,更多內容可以參考:php 檔案頭部(header)資訊詳解 。

舉一些具體的例子。

例1: 本例使瀏覽器重新導向到 PHP 的官方網站。

  1. Header("Location: http://www.php.net"); exit;
複製代碼

例2: 要使用者每次都能得到最新的資料,而不是 Proxy 或 cache 中的資料,可以使用下列的標題

  1. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  2. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); header("Cache-Control: no-cache, must-revalidate");
  3. header("Pragma: no-cache");
複製代碼

例3: 讓使用者的瀏覽器出現找不到檔案的資訊。

  1. header("Status: 404 Not Found");
複製代碼

例4:讓使用者下載檔案。

  1. header("Content-type: application/x-gzip");
  2. header("Content-Disposition: attachment; filename=檔案名稱");
  3. header("Content-Description: PHP3 Generated Data");
複製代碼

說明:不管頁面有多少header,它會執行最後一個,不過是有條件的,例如:

  1. header('Location:http://bbs.it-home.org');
  2. header('Location:http://www.g.cn');
  3. header('Location:http://www.baidu.com');
  4. //跳到百度
  5. header('Location:http://bbs.it-home.org');echo '程式員之家';
  6. header('Location:http://www.g.cn');
  7. header('Location:http://www.baidu.com');
  8. //跳到google
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.