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");