PHP Header常用指令講解

來源:互聯網
上載者:User

標籤:

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常用指令講解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.