简体   繁体   English

如何控制浏览器缓存?

[英]How to control browser cache?

I'm working on a website, where the audience isn't very comfortable with computers. 我在一个网站上工作,那里的听众对计算机不太满意。 A complaint received often in the past was about (minor) new updates, though important ones not being visible (like date and times being changed). 过去经常收到的投诉是关于(较小)新更新的,尽管重要的更新是不可见的(例如,日期和时间已更改)。 Problem: browser caching and people not knowing how to refresh a browser. 问题:浏览器缓存,人们不知道如何刷新浏览器。 Because of the rather large audience, it will be impossible explaining that to all of them. 由于受众众多,因此不可能对所有人进行解释。

We'll be putting an entire new site online very soon, so I suspect that this change will cause the browser to get the update...am I right on this one? 我们将很快将整个新网站上线,因此我怀疑此更改将导致浏览器获得更新...我是对的吗?

But in order to prevent the complaints of the past I would like a better control of the user's browser caching. 但是为了防止过去的抱怨,我希望更好地控制用户的浏览器缓存。

It would be best to put a caching time (between a day and one or two weeks) per page, because for the general info caching is a good thing, because it isn't updated that often and it isn't as sensitive to time as our calender is! 最好每页设置一个缓存时间(一天到一两周之间),因为对于一般信息而言,缓存是一件好事,因为它不经常更新并且对时间不那么敏感正如我们的压延机一样! But I want a new version of the calender within a day from the last visit. 但是,我希望在上次访问后的一天内获得新版本的压光机。 --> I would even consider caching parts of pages if possible (header and footer are less likely to change then content etc.) ->如果可能的话,我什至会考虑缓存页面的某些部分(页眉和页脚不太可能更改内容,而后更改内容等)。

What's the best approach? 最好的方法是什么? Should I change something on the server settings (as far as the hosting allows me to) and how can I do this (hosting control panel, php.ini)? 我是否应该在服务器设置上进行某些更改(就主机托管而言),我该如何做(托管控制面板php.ini)?

Or should I do something in PHP? 还是我应该在PHP中做些什么? Or in the HTML like I found over here: HTML Cache control 或者像我在此处找到的HTML中那样: HTML缓存控件

Or maybe a combination of some/all of the above? 还是上述某些/全部的组合?

You can either do this with PHP or HTML 您可以使用PHPHTML进行此操作

PHP 的PHP

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

HTML 的HTML

<meta http-equiv="expires" content="Day, 01 Mon 2014 00:00:00 GMT"/>
<meta http-equiv="pragma" content="no-cache" />

make sure to put that in your main include file for php or html to do this for all pages. 请确保将其放在您的主要包含文件中,用于php或html,以对所有页面执行此操作。 I would use the php header over html. 我会在html上使用php标头。

You can achieve this by .htaccess: 您可以通过.htaccess实现此目的:

<filesMatch "\.(php|html|htm|js|css)$">
  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>

This will expire the server cache and each request will get the new pages 这将使服务器缓存过期,并且每个请求都将获取新页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM