簡體   English   中英

標頭緩存控件不起作用標頭“最后修改”

[英]Header Cache-Control do not work header “Last-Modified”

我用php制作圖像,並希望控制緩存時間。 我有以下代碼:

header("Cache-Control: must-revalidate"); 

$fn = gmdate('D, d M Y H:i:s \G\M\T', time() + 60);
$now = gmdate('D, d M Y H:i:s \G\M\T', time());

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
    strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) <= $now  )
{
        // Client's cache IS current, so we just respond '304 Not Modified'.
        header('Last-Modified: '.$fn, true, 304);
}else {
        // Image not cached or cache outdated, we respond '200 OK' and output the image.
        header('Last-Modified: '.$fn, true, 200);
        //Header
        header("Content-type: image/PNG");
        //Ausgeben
        imagePNG($bild);

};

它應該僅在60秒后給出新圖像。 但是我的代碼總是給它。

我認為您的運算法則已失效; 根據您的代碼查看以下示例:

$lifetime = 60;

header("Cache-Control: must-revalidate");

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  $lastMod = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
} else {
  $lastMod = 0;
}

if ($lastMod <= $_SERVER['REQUEST_TIME'] - $lifetime) {
  // Time to refresh
  $lastMod = $_SERVER['REQUEST_TIME'];
  header("Content-type: text/plain");
  header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $lastMod), true, 200);
  echo "Hello!";

} else {
  header("Last-Modified: " . gmdate('D, d M Y H:i:s \G\M\T', $lastMod), true, 304);
}

這會將最后修改的標頭設置為現在(使用$_SERVER['REQUEST_TIME']可能比直接使用time()更高效),並在后續請求中檢查If-Modified-Since是否至少60秒大。 如果是這樣,它將刷新(並將上一次修改的值重置為現在); 否則,返回304,並且最后修改不變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM