PHP header()

header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more information on HTTP headers.

Header就是HTML的標頭,用來告訴瀏覽器該頁面的相關資訊
是個很重要的功能,他可以強制規定(或欺騙)瀏覽器的瀏覽方式 .

設定頁面編碼

header('Content-Type: text/html; charset=utf-8'); //使用utf-8編碼

跳轉到網頁(以google為例)

header('Location: https://www.google.com/');

輸出類型(例如:pdf)

 header('Content-Type: application/pdf'); 

設定狀態為404(詳情請見http 錯誤代碼)

header(‘HTTP/1.1 404 Not Found’); 
header(“status: 404 Not Found”); 

不使用快取

header(“Cache-Control: no-cache, must-revalidate”);  

下載檔案(同時兩行都要有,不然有機會失敗)

header('Content-type:application/force-download');
header('Content-Disposition: attachment; filename="test.txt"');

參考:
https://www.php.net/manual/en/function.header.php