PHP header 函數教程

來源:互聯網
上載者:User

PHP header 函數教程

定義和用法
header ( )函數發送一個原始HTTP頭到用戶端。

重要的是要看到,標題( )必須在任何所謂的實際產出發送(在PHP 4和以後,您可以使用輸出緩衝來解決這個問題) :

<?php
// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?>

文法:

header(string,replace,http_response_code)
  
Parameter Description
string
必需的。指定的標題字串發送
replace
任擇。指示是否標題應取代以前或添加第二個標題。預設值是true (將取代) 。假(允許多個標題同一類型)
http_response_code
任擇。部隊的HTTP響應代碼到指定的值(可在PHP 4.3和更高)
  

提示和說明
註:自PHP 4.4這一功能可以防止一個以上的標題發送一次。這是一個保護,防止頭注入攻擊。




範例1
防止頁面緩衝:

 

// Date in the pastheader("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Cache-Control: no-cache");header("Pragma: no-cache");

 

樣本2
讓使用者將提示儲存產生的PDF檔案(內容處置標題是用來提供建議的檔案名稱,並迫使瀏覽器來顯示儲存對話方塊) :

 

header("Content-type:application/pdf");

// It will be called downloaded.pdfheader("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source is in original.pdfreadfile("original.pdf");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.