随机版本号与图像强制缓存


Random versioning number with images to force cache

我随机使用几个php脚本,其中包含HTML代码的各个部分,样式表和图像。但是最近我遇到了一个缓存问题。

我给样式表添加了一个随机的版本号来强制它成为一个新的样式表。

    <script>document.write('<link rel="stylesheet" href="/css/fonts/rotate.php?dev=' + Math.floor(Math.random() * 100) + '" type="text/css" media="screen" />');</script> 

如何对图像做类似的事情?

<script>
document.write("<IMG SRC=" + "/images/rotate.php" + ">");
</script>

谢谢

您可以添加以下标题到rotate.php:

  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
  header("Cache-Control: no-store, no-cache, must-revalidate"); 
  header("Cache-Control: post-check=0, pre-check=0", false); 
  header("Pragma: no-cache"); 

你也可以尝试通过。htaccess文件:

<Directory />
    <filesMatch "rotate.(html|htm|js|css|php)$">
    FileETag None
    <ifModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </ifModule>
    </filesMatch>
</Directory>

您可以在末尾添加随机的乱码参数。

"/images/rotate.php?dev=" + Math.floor(Math.random() * 101)