標籤:
PHP中header分為三部分:
第一部分:HTTP協議的版本(HTTP-Version);
第二部分:狀態碼(Status);
第三部分:原因短語(Reason-Phrase)。
範例程式碼:
用header指令返回頁面HTTP資訊
1、請求頁面正常返回
header(‘HTTP/1.1 200 OK‘);
2、頁面沒找到
header(‘HTTP/1.1 404 Not Found‘);
3、301永久轉向
header(‘HTTP/1.1 301 Moved Permanently‘);
4、訪問受限
header(‘HTTP/1.1 403 Forbidden‘);
5、伺服器錯誤
header(‘HTTP/1.1 500 Internal Server Error‘);
6、重新導向到一個新的地址
header(‘Location: http://www.abcd9.com/‘);
延遲一段時間後重新導向
header(‘Refresh: 10; url=http://www.abcd9.com/‘); //延遲10秒
覆蓋 X-Powered-By value
header(‘X-Powered-By: PHP/4.4.0‘);
header(‘X-Powered-By: Brain/0.6b‘);
內容語言 (en = English)
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: application/octet-stream‘);
header(‘Content-Disposition: attachment; filename="abcd9.zip"‘);
header(‘Content-Transfer-Encoding: binary‘);
禁止緩衝當前文檔
header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate‘);
header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘);
設定內容類型
header(‘Content-Type: text/html; charset=utf-8‘); //編碼為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‘); //MPEG (MP3,…)音頻檔案
header(‘Content-Type: application/x-shockwave-flash‘); //flash檔案
顯示登入對話方塊,可以用來進行HTTP認證
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‘;
PHP Header常用指令講解