简体   繁体   English

如何阻止浏览器缓存动态内容

[英]how to stop browser from caching dynamic content

I have php generated pages that change every few minutes based on the underlying data. 我有php生成的页面,每隔几分钟就会根据底层数据进行更改。 All is good until a user follows a link. 一切都很好,直到用户关注链接。 When the user clicks the back button on their browser to return, the previously loaded version of the page is displayed. 当用户单击其浏览器上的后退按钮以返回时,将显示先前加载的页面版本。 The browser is not reloading the page from the server. 浏览器没有从服务器重新加载页面。

In order to get the new content from the server, users must click reload. 要从服务器获取新内容,用户必须单击“重新加载”。

I tried the normal meta tags, and outputting header() from php. 我尝试了普通的meta标签,并从php输出header()。

The behavior is the same in IE, FF and Chrome. IE,FF和Chrome中的行为相同。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
  Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">

Try setting these headers 尝试设置这些标头

header("Cache-control: no-store, no-cache, must-revalidate");
header("Expires: Mon, 26 Jun 1997 05:00:00 GMT");
header("Pragma: no-cache");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

This tells the browser not to cache the page and so it should reload when they hit back. 这告诉浏览器不要缓存页面,所以它应该在它们回击时重新加载。

There is no ONE solution for all browsers. 所有浏览器都没有一个解决方案。

No matter what headers you use some browsers seem to always cache! 无论你使用什么标头,一些浏览器似乎总是缓存!

I just came up with this solution that I myself was looking for. 我刚刚提出了我自己正在寻找的解决方案。 It was staring me in the face the whole time. 它一直盯着我。

This does not stop the browser from caching the data, it only stops the stale data from being re-read from the cache after the page has been reloaded. 这不会阻止浏览器缓存数据,它只会在页面重新加载后停止从缓存中重新读取过时数据。 You can still use those headers if the data is sensitive and never want it written to cache, but it will not work in ALL cases. 如果数据是敏感的并且从不希望将其写入缓存,您仍然可以使用这些标头,但它并不适用于所有情况。 In my application that is not necessary I want to only avoid loading stale data from the browsers cache. 在我不需要的应用程序中,我只想避免从浏览器缓存中加载陈旧数据。

this solution is AMAZINGLY simple and requires little expertise to implement . 这个解决方案非常简单,只需要很少的专业知识即可实现。

I use php but I believe URL variables can be used with asp, javascript and much more 我使用php,但我相信URL变量可以与asp,javascript等等一起使用

Your browser sees http://example.com/index.php , http://example.com/index.php?x=32 and http://example.com/index.php?x=3199 all as different URLs so it will not use any of the above URLs as a cache for the other. 您的浏览器看到http://example.com/index.phphttp://example.com/index.php?x=32http://example.com/index.php?x=3199所有不同的URL所以它不会使用任何上述URL作为另一个的缓存。

I generate a random number in PHP which you can probably do in ASP 我在PHP中生成一个随机数,你可以在ASP中做

in php I use: 在PHP中我使用:

$rand=(rand(1, 99999));

Now my link in PHP (should be easy to understand even with limited PHP) 现在我在PHP中的链接(即使使用有限的PHP也应该很容易理解)

'<a href="http://example.com/index.php?rand='.$rand.'>"

If the page already has URL variables then we add it to any GET forms, or concatenated links. 如果页面已经有URL变量,那么我们将它添加到任何GET表单或连接链接。

If the Forms are post forms we tag it along the "action" URL, so 如果表单是帖子表单,我们会在“操作”URL上标记它,所以

http://example.com/index.php 

becomes

http://example.com/index.php?rand=<?php echo $rand;?>

Then any page I do not want cached I simply add this random number as a URL variable. 然后我不想缓存的任何页面我只是将这个随机数添加为URL变量。 That URL variable is not handled by the server at all , I never GET that number and do not need to. 该URL变量根本不是由服务器处理的,我从来没有得到那个数字而且不需要。

http://example.com/index.php?rand=4398 next time we load the same page the browser believes it is a different page due to the different rand= URL variable. http://example.com/index.php?rand=4398下次我们加载同一页面时,由于rand = URL变量不同,浏览器认为它是一个不同的页面。

No worries we never have to read it, it is only to "fool" the browser. 不用担心我们永远不必阅读它,它只是“愚弄”浏览器。 The next timne we go to the same page we will most probably see a very different number 下一个我们去同一页面的时间,我们很可能会看到一个非常不同的数字

http://example.com/index.php?rand=55468 http://example.com/index.php?rand=55468

or as far as your browser is concerned, NOT the same page, even if we completely discard the variable back at the server , meaning it has no value in your ASP or PHP and never used as a variable. 或者就你的浏览器而言,不是相同的页面,即使我们完全丢弃了服务器上的变量,这意味着它在你的ASP或PHP中没有任何价值,也从未用作变量。

The answer is now so simple I am surpried I spent weeks on this and nothing worked consistently. 答案现在很简单,我被匆匆忙忙地花了几周时间,没有任何工作能够持续下去。 THIS DOES! 这样做!

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

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