简体   繁体   中英

how to stop browser from caching dynamic content

I have php generated pages that change every few minutes based on the underlying data. 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.

The behavior is the same in IE, FF and 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

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.

I generate a random number in PHP which you can probably do in ASP

in php I use:

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

Now my link in PHP (should be easy to understand even with limited 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.

If the Forms are post forms we tag it along the "action" URL, so

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. That URL variable is not handled by the server at all , I never GET that number and do not need to.

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.

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

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.

The answer is now so simple I am surpried I spent weeks on this and nothing worked consistently. THIS DOES!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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