溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

HTTP消息頭網(wǎng)頁(yè)緩存控制以及header常用指令的示例分析

發(fā)布時(shí)間:2021-10-12 13:54:57 來(lái)源:億速云 閱讀:115 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹HTTP消息頭網(wǎng)頁(yè)緩存控制以及header常用指令的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

網(wǎng)頁(yè)的緩存是由HTTP消息頭中的“Cache-control”來(lái)控制的,常見(jiàn)的取值有private、no-cache、max-age、must-revalidate等,默認(rèn)為private。其作用根據(jù)不同的重新瀏覽方式分為以下幾種情況:
(1) 打開(kāi)新窗口
值為private、no-cache、must-revalidate,那么打開(kāi)新窗口訪問(wèn)時(shí)都會(huì)重新訪問(wèn)服務(wù)器
而如果指定了max-age值,那么在此值內(nèi)的時(shí)間里就不會(huì)重新訪問(wèn)服務(wù)器,例如:
Cache-control: max-age=5(表示當(dāng)訪問(wèn)此網(wǎng)頁(yè)后的5秒內(nèi)再次訪問(wèn)不會(huì)去服務(wù)器)
(2) 在地址欄回車(chē)
值為private或must-revalidate則只有第一次訪問(wèn)時(shí)會(huì)訪問(wèn)服務(wù)器,以后就不再訪問(wèn)。
值為no-cache,那么每次都會(huì)訪問(wèn)。
值為max-age,則在過(guò)期之前不會(huì)重復(fù)訪問(wèn)。
(3) 按后退按扭
值為private、must-revalidate、max-age,則不會(huì)重訪問(wèn),
值為no-cache,則每次都重復(fù)訪問(wèn)
(4) 按刷新按扭
無(wú)論為何值,都會(huì)重復(fù)訪問(wèn)
Cache-control值為“no-cache”時(shí),訪問(wèn)此頁(yè)面不會(huì)在Internet臨時(shí)文章夾留下頁(yè)面?zhèn)浞荨?br/>另外,通過(guò)指定“Expires”值也會(huì)影響到緩存。例如,指定Expires值為一個(gè)早已過(guò)去的時(shí)間,那么訪問(wèn)此網(wǎng)時(shí)若重復(fù)在地址欄按回車(chē),那么每次都會(huì)重復(fù)訪問(wèn): Expires: Fri, 31 Dec 1999 16:00:00 GMT
比如:禁止頁(yè)面在IE中緩存
http響應(yīng)消息頭部設(shè)置:
CacheControl = no-cache
Pragma=no-cache
Expires = -1
Expires是個(gè)好東東,如果服務(wù)器上的網(wǎng)頁(yè)經(jīng)常變化,就把它設(shè)置為-1,表示立即過(guò)期。如果一個(gè)網(wǎng)頁(yè)每天凌晨1點(diǎn)更新,可以把Expires設(shè)置為第二天的凌晨1點(diǎn)。
當(dāng)HTTP1.1服務(wù)器指定CacheControl = no-cache時(shí),瀏覽器就不會(huì)緩存該網(wǎng)頁(yè)。
舊式 HTTP 1.0 服務(wù)器不能使用 Cache-Control 標(biāo)題。
所以為了向后兼容 HTTP 1.0 服務(wù)器,IE使用Pragma:no-cache 標(biāo)題對(duì) HTTP 提供特殊支持。
如果客戶端通過(guò)安全連接 (https://)/與服務(wù)器通訊,且服務(wù)器在響應(yīng)中返回 Pragma:no-cache 標(biāo)題,
則 Internet Explorer不會(huì)緩存此響應(yīng)。注意:Pragma:no-cache 僅當(dāng)在安全連接中使用時(shí)才防止緩存,如果在非安全頁(yè)中使用,處理方式與 Expires:-1相同,該頁(yè)將被緩存,但被標(biāo)記為立即過(guò)期。

header常用指令
header分為三部分:
第一部分為HTTP協(xié)議的版本(HTTP-Version);
第二部分為狀態(tài)代碼(Status);
第三部分為原因短語(yǔ)(Reason-Phrase)。

復(fù)制代碼 代碼如下:


// fix 404 pages:   用這個(gè)header指令來(lái)解決URL重寫(xiě)產(chǎn)生的404 header
header('HTTP/1.1 200 OK');

// set 404 header:   頁(yè)面沒(méi)找到
header('HTTP/1.1 404 Not Found');

//頁(yè)面永久重定向,可以告訴搜索引擎更新它們的urls
// set Moved Permanently header (good for redrictions)
// use with location header
header('HTTP/1.1 301 Moved Permanently');

// 訪問(wèn)受限
header('HTTP/1.1 403 Forbidden');

// 服務(wù)器錯(cuò)誤
header('HTTP/1.1 500 Internal Server Error');

// 重定向到一個(gè)新的位置
// redirect to a new location:
header('Location: http://www.www.jb51.net);

延遲一段時(shí)間后重定向
// redrict with delay:
header('Refresh: 10; url=https://www.jb51.net');
print 'You will be redirected in 10 seconds';

// 覆蓋 X-Powered-By value
// override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');

// 內(nèi)容語(yǔ)言 (en = English)
// content language (en = English)
header('Content-language: en');

//最后修改時(shí)間(在緩存的時(shí)候可以用到)
// last modified (good for caching)
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

// 告訴瀏覽器要獲取的內(nèi)容還沒(méi)有更新
// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');

// 設(shè)置內(nèi)容的長(zhǎng)度 (緩存的時(shí)候可以用到):
// set content length (good for caching):
header('Content-Length: 1234');

// 用來(lái)下載文件:
// Headers for an download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');

// 禁止緩存當(dāng)前文檔:
// load the file to send:readfile('example.zip');
// Disable caching of the current document:
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

// 設(shè)置內(nèi)容類型:
// Date in the pastheader('Pragma: no-cache');
// set content type:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
// plain text file
header('Content-Type: text/plain');

// JPG picture
header('Content-Type: image/jpeg');

// ZIP file
header('Content-Type: application/zip');

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

// Audio MPEG (MP3,...) file
header('Content-Type: audio/mpeg');

// Flash animation// show sign in box
header('Content-Type: application/x-shockwave-flash');

// 顯示登錄對(duì)話框,可以用來(lái)進(jìn)行HTTP認(rèn)證
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';

以上是“HTTP消息頭網(wǎng)頁(yè)緩存控制以及header常用指令的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI