一、header()函數的做用是:發送一個原始 HTTP 標頭[Http Header]到客戶端。html
header(string,replace,http_response_code) /string:必需。規定要發送的報頭字符串。 replace:可選。指示該報頭是否替換以前的報頭,或添加第二個報頭。 默認是 true(替換)。false(容許相同類型的多個報頭)。 http_response_code:可選。把 HTTP 響應代碼強制爲指定的值。/瀏覽器
注意:必須在任何實際的輸出被髮送以前調用 header() 函數。緩存
二、 用法1:跳轉頁面服務器
header("Location:https://baidu.com"); //正常跳轉 header('Refresh: 3; url=https://www.baidu.com'); //3s後跳轉 //在header做跳轉時,避免發生錯誤後,代碼繼續執行,通常加個exit;app
用法2:聲明content-type(我常常拿來決解亂碼)函數
header('content-type:text/html;charset=utf-8');post
用法3:返回響應狀態碼url
header('HTTP/1.1 403 Forbidden');code
用法4:執行下載操做(隱藏文件的位置)htm
header('Content-Type: application/octet-stream'); //設置內容類型 header('Content-Disposition: attachment; filename="example.zip"');//設置MIME用戶做爲附件 header('Content-Transfer-Encoding: binary'); //設置傳輸方式 header('Content-Length: '.filesize('example.zip')); //設置內容長度
用法5:控制瀏覽器緩存
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
//若是服務器上的網頁常常變化,就把它設置爲-1,表示當即過時
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' );