如何防止css文件中的图像被缓存?

时间:2014-03-04 21:18:24

标签: php html css caching

如何防止浏览器缓存css文件中的x.jpg? 这些图像必须每天刷新。

CSS文件:

#slider{
    background: url(1.jpg) 0 0 no-repeat,
                url(2.jpg) 313px 0 no-repeat,
                url(3.jpg) 626px 0 no-repeat,
                url(4.jpg) 939px 0 no-repeat,
                url(5.jpg) 1252px 0 no-repeat,
                url(6.jpg) 1565px 0 no-repeat,
                url(7.jpg) 1878px 0 no-repeat,
                url(8.jpg) 2191px 0 no-repeat;
}

我在我的php文件中尝试了这个,但我不知道它为什么不起作用:

PHP文件

<script>
$("#slider").css(
      "background",
      "url(1.jpg?v=<?php echo time(); ?>) 0 0 no-repeat,"      +
      "url(2.jpg?v=<?php echo time(); ?>) 313px 0 no-repeat,"  +
      "url(3.jpg?v=<?php echo time(); ?>) 626px 0 no-repeat,"  +
      "url(4.jpg?v=<?php echo time(); ?>) 939px 0 no-repeat,"  +
      "url(5.jpg?v=<?php echo time(); ?>) 1252px 0 no-repeat," +
      "url(6.jpg?v=<?php echo time(); ?>) 1565px 0 no-repeat," +
      "url(7.jpg?v=<?php echo time(); ?>) 1878px 0 no-repeat," +
      "url(8.jpg?v=<?php echo time(); ?>) 2191px 0 no-repeat"
);
</script>

1 个答案:

答案 0 :(得分:0)

[我的问题的答案]

我在我的图像目录中添加了这个.htaccess文件:

.htaccess文件

<filesMatch "\.(jpg)$">
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>