首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP header() 函數常用 HTTP 標頭設定速查表:狀態碼、重導向、快取控制、內容型別與下載

     編輯:PHP 綜合知識

HTTP 狀態碼

header('HTTP/1.1 200 OK');
header('HTTP/1.1 404 Not Found');
header('HTTP/1.1 301 Moved Permanently');

重導向

header('Location: https://example.com/');
header('Refresh: 10; url=https://example.com/'); // 10 秒後跳轉
echo 'You will be redirected in 10 seconds';

自訂標頭

header('X-Powered-By: PHP/7.4');
header('Content-language: en');

快取控制

$time = time() - 60;
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $time) . ' GMT');
header('HTTP/1.1 304 Not Modified'); // 告知瀏覽器使用快取

// 禁用快取
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Pragma: no-cache');

內容長度與下載

header('Content-Length: 1234');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
readfile('example.zip');

常見 Content-Type 對照表

類型header() 設定
HTML (UTF-8)header('Content-Type: text/html; charset=utf-8');
HTML (ISO-8859-1)header('Content-Type: text/html; charset=iso-8859-1');
純文字header('Content-Type: text/plain');
JSONheader('Content-Type: application/json');
XMLheader('Content-Type: text/xml');
CSSheader('Content-Type: text/css');
JavaScriptheader('Content-Type: text/javascript');
JPEGheader('Content-Type: image/jpeg');
PNGheader('Content-Type: image/png');
GIFheader('Content-Type: image/gif');
PDFheader('Content-Type: application/pdf');
ZIPheader('Content-Type: application/zip');
音訊 (MP3)header('Content-Type: audio/mpeg');
Flashheader('Content-Type: application/x-shockwave-flash');
Atomheader('Content-Type: application/atom+xml');
RSSheader('Content-Type: application/rss+xml; charset=ISO-8859-1');

HTTP 基礎認證

header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
echo 'Text shown if user cancels or enters wrong credentials';
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved