先看看官方文檔的定義javascript
(PHP 4, PHP 5, PHP 7)php
header — 發送原生 HTTP 頭css
1 void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
參數:html
stringjava
有兩種特別的頭。第一種以"HTTP/"開頭的 (case is not significant),將會被用來計算出將要發送的HTTP狀態碼。 例如在 Apache 服務器上用 PHP 腳原本處理不存在文件的請求(使用 ErrorDocument 指令), 就會但願腳本響應了正確的狀態碼。json
1 <?php 2 header("HTTP/1.0 404 Not Found"); 3 ?>
第二種特殊狀況是"Location:"的頭信息。它不只把報文發送給瀏覽器,並且還將返回給瀏覽器一個 REDIRECT(302)的狀態碼,除非狀態碼已經事先被設置爲了201或者3xx。瀏覽器
1 <?php 2 header("Location: http://www.example.com/"); /* Redirect browser */
3
4 /* Make sure that code below does not get executed when we redirect. */
5 exit; 6 ?>
replace
可選參數 replace
代表是否用後面的頭替換前面相同類型的頭。 默認狀況下會替換。若是傳入 FALSE
,就能夠強制使相同的頭信息並存。例如:緩存
1 <?php 2 header('WWW-Authenticate: Negotiate'); 3 header('WWW-Authenticate: NTLM', false); 4 ?>
http_response_code服務器
強制指定HTTP響應的值。注意,這個參數只有在報文字符串(string
)不爲空的狀況下才有效。app
header函數的常見用處有如下幾點:
一、重定向
header('Location: http://www.example.com/');
二、指定內容:
header('Content-type: application/pdf');
三、附件:
header('Content-type: application/pdf');
//指定內容爲附件,指定下載顯示的名字
header('Content-Disposition: attachment; filename="downloaded.pdf"');
//打開文件,並輸出
readfile('original.pdf');
以上代碼能夠在瀏覽器產生文件對話框的效果
四、讓用戶獲取最新的資料和數據而不是緩存
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 設置臨界時間
詳細例子:
1 <?php 2 header('HTTP/1.1 200 OK'); // ok 正常訪問
3 header('HTTP/1.1 404 Not Found'); //通知瀏覽器 頁面不存在
4 header('HTTP/1.1 301 Moved Permanently'); //設置地址被永久的重定向 301
5 header('Location: http://www.ithhc.cn/'); //跳轉到一個新的地址
6 header('Refresh: 10; url=http://www.ithhc.cn/'); //延遲轉向 也就是隔幾秒跳轉
7 header('X-Powered-By: PHP/6.0.0'); //修改 X-Powered-By信息
8 header('Content-language: en'); //文檔語言
9 header('Content-Length: 1234'); //設置內容長度
10 header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器最後一次修改時間
11 header('HTTP/1.1 304 Not Modified'); //告訴瀏覽器文檔內容沒有發生改變
12
13 ###內容類型###
14 header('Content-Type: text/html; charset=utf-8'); //網頁編碼
15 header('Content-Type: text/plain'); //純文本格式
16 header('Content-Type: image/jpeg'); //JPG、JPEG
17 header('Content-Type: application/zip'); // ZIP文件
18 header('Content-Type: application/pdf'); // PDF文件
19 header('Content-Type: audio/mpeg'); // 音頻文件
20 header('Content-type: text/css'); //css文件
21 header('Content-type: text/javascript'); //js文件
22 header('Content-type: application/json'); //json
23 header('Content-type: application/pdf'); //pdf
24 header('Content-type: text/xml'); //xml
25 header('Content-Type: application/x-shockw**e-flash'); //Flash動畫
26
27 ######
28
29 ###聲明一個下載的文件###
30 header('Content-Type: application/octet-stream'); 31 header('Content-Disposition: attachment; filename="ITblog.zip"'); 32 header('Content-Transfer-Encoding: binary'); 33 readfile('test.zip'); 34 ######
35
36 ###對當前文檔禁用緩存###
37 header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); 38 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 39 ######
40
41 ###顯示一個須要驗證的登錄對話框###
42 header('HTTP/1.1 401 Unauthorized'); 43 header('WWW-Authenticate: Basic realm="Top Secret"'); 44 ######
45
46
47 ###聲明一個須要下載的xls文件###
48 header('Content-Disposition: attachment; filename=ithhc.xlsx'); 49 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 50 header('Content-Length: '.filesize('./test.xls')); 51 header('Content-Transfer-Encoding: binary'); 52 header('Cache-Control: must-revalidate'); 53 header('Pragma: public'); 54 readfile('./test.xls'); 55 ######
56 ?>