-
重定向
Header("Location: http://url";);
-
禁止頁面緩存
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 -
讓使用者的瀏覽器出現找不到檔案的信息
header(”http/1.1 404 Not Found”);
-
讓使用者下載檔案( 隱藏文件的位置 )
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=文件名\");
header("Content-Description: PHP3 Generated Data"); -
header函數前輸入內容
header(”http/1.1 403 Forbidden”);
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到瀏覽器,然后再發送輸出緩沖中的內容。 -
延遲轉向
header('Refresh: 10; url=http://url/');
-
文檔語言
header('Content-language: en')
-
告訴瀏覽器最后一次修改時間
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); -
告訴瀏覽器文檔內容沒有發生改變
header('HTTP/1.1 304 Not Modified');
-
設置內容長度
header('Content-Length: 1234');
-
設置內容類型:
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';