0
我試圖構建一個用PHP編寫的瀏覽器緩存控制系統。Php中的瀏覽器緩存控制系統
在深多了,我想服務用PHP每一個瀏覽器請求,以產生正確的HTTP響應頭的以產生HTTP 200或HTTP 304未修改在合適的時間。
最大的問題是:我如何委派PHP來檢查資源是HTTP 200還是HTTP 304?
我試圖構建一個用PHP編寫的瀏覽器緩存控制系統。Php中的瀏覽器緩存控制系統
在深多了,我想服務用PHP每一個瀏覽器請求,以產生正確的HTTP響應頭的以產生HTTP 200或HTTP 304未修改在合適的時間。
最大的問題是:我如何委派PHP來檢查資源是HTTP 200還是HTTP 304?
下面是一些示例代碼,用於管理PHP提供頁面的瀏覽器緩存。您需要確定一個時間戳$myContentTimestamp
,指出頁面上內容的上次修改時間。
// put this line above session_start() to disable PHP's default behaviour of disabling caching if you're using sessions
session_cache_limiter('');
$myContentTimestamp = 123456789; // here, get the last modified time of the content on this page, ex. a DB record or file modification timestamp
// browser will send $_SERVER['HTTP_IF_MODIFIED_SINCE'] if it has a cache of your page
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $myContentTimestamp) {
// browser's cache is the latest version, so tell the browser there is no newer content and exit
header('HTTP/1.1 304 Not Modified');
header('Last-Modified: ' . date("D M j G:i:s T Y", $myContentTimestamp));
die;
} else {
// tell the browser the last change date of this page's content
header('Last-Modified: ' . date("D M j G:i:s T Y", $myContentTimestamp));
// tell the browser it has to ask if there are any changes whenever it shows this page
header("Cache-Control: must-revalidate");
// now show your page
echo "hello world! This page was last modified on " . date('r', $myContentTimestamp);
}
任何網絡服務器都會將它用於靜態文件。如果你想用php做,請檢查文件和時間戳 –
電子標籤的散列? http://en.wikipedia.org/wiki/HTTP_ETag –