How to clear browser cache with php?
Answers

Thanks @tim!
The problem was in the headers i was serving alongside my media. I looked at the response in Chrome and it said:
Cache-Control: must-revalidate, no-cache, no-store
I didn't set these, it must be a default of apache since I'm serving through a script? Anyways, to fix, I just added:
Cache-Control: public
to my PHP file and it seems to be working!
The problem with your approach is that you are setting it where it is already too late for MVC to apply it. The following three lines of your code should be put in the method that shows the view (consequently the page) that you do not want to show.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
If you want to apply the "no cache on browser back" behavior on all pages then you should put it in global.asax.
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
Caching is controlled via a variety of HTTP headers. You should read Mark Nottingham's Caching Tutorial for Web Authors and Webmasters. You can set HTTP headers for documents outputted from PHP using the header function.
Have a look at Content Expiration, that should do the trick for you on the caching.
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/0fc16fe7-be45-4033-a5aa-d7fda3c993ff.mspx?mfr=true
To set the expiration of Web site content
In IIS Manager, double-click the local computer; right-click the Web Sites folder, an individual Web site folder, a virtual directory, or a file; and then click Properties.
Click the HTTP Headers tab.
Select the Enable content expiration check box.
Click Expire immediately, Expire after, or Expire on, and type the appropriate expiration information in the corresponding boxes.
Click OK.
A search here on SO would have returned some good information - like Leverage browser caching - but anyway...
From: http://www.samaxes.com/2011/05/improving-web-performance-with-apache-and-htaccess/
A first-time visitor to your page will make several HTTP requests to download all your sites files, but by using the
Expires
andCache-Control
headers you make those files cacheable. This avoids unnecessary HTTP requests on subsequent page views.
Apache enables those headers thanks to mod_expires
and mod_headers
modules.
The mod_expires
module controls the setting of the Expires
HTTP header and the max-age
directive of the Cache-Control
HTTP header in server responses.
To modify Cache-Control
directives other than max-age
, you can use the mod_headers
module.
The mod_headers
module provides directives to control and modify HTTP request and response headers. Headers can be merged, replaced or removed.
Rule for setting Expires
headers:
# BEGIN Expire headers
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers
Rule for setting Cache-Control
headers:
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch ".(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch ".(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch ".(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch ".(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
Note: There is no need to set max-age
directive with Cache-Control
header since it is already set by mod_expires
module.
must-revalidate
means that once a response becomes stale it has to be revalidated; it doesn’t mean that it has to be checked every time.
More info here: http://www.mnot.net/cache_docs/
And from Google: http://code.google.com/speed/page-speed/docs/caching.html
And Yahoo: http://developer.yahoo.com/performance/rules.html#expires
Currently, there is no way to clear the cache through the web driver API. However, if you can start a new instance of the browser each time, the cache should be cleared in FF and Chrome because a new profile is created on each launch.
The comments for issue #40 (Clear cache) in the Selenium issue tracker list two potential solutions to your problem if creating a new browser instance isn't possible:
- Clear the IE cache from the command line
- Disable the FF cache using a custom profile
HTH