Our investigations have shown us that not all browsers respect the HTTP cache directives in a uniform manner.
For security reasons we do not want certain pages in our application to be cached, ever, by the web browser. This must work for at least the following browsers:
- Internet Explorer 6+
- Firefox 1.5+
- Safari 3+
- Opera 9+
- Chrome
Our requirement came from a security test. After logging out from our website you could press the back button and view cached pages.
I found the web.config route useful (tried to add it to the answer but doesn't seem to have been accepted so posting here)
And here is the express / node.js way of doing the same:
DISCLAIMER: I strongly suggest reading @BalusC's answer. After reading the following caching tutorial: http://www.mnot.net/cache_docs/ (I recommend you read it, too), I believe it to be correct. However, for historical reasons (and because I have tested it myself), I will include my original answer below:
I tried the 'accepted' answer for PHP, which did not work for me. Then I did a little research, found a slight variant, tested it, and it worked. Here it is:
That should work. The problem was that when setting the same part of the header twice, if the
false
is not sent as the second argument to the header function, header function will simply overwrite the previousheader()
call. So, when setting theCache-Control
, for example if one does not want to put all the arguments in oneheader()
function call, he must do something like this:See more complete documentation here.
The headers in the answer provided by BalusC does not prevent Safari 5 (and possibly older versions as well) from displaying content from the browser cache when using the browser's back button. A way to prevent this is to add an empty onunload event handler attribute to the body tag:
This hack apparently breaks the back-forward cache in Safari: Is there a cross-browser onload event when clicking the back button?
I had no luck with
<head><meta>
elements. Adding HTTP cache related parameters directly (outside of the HTML doc) does indeed work for me.Sample code in Python using web.py
web.header
calls follows. I purposefully redacted my personal irrelevant utility code.I've had best and most consistent results across all browsers by setting Pragma: no-cache