user1528274
user1528274

Reputation: 13

Stopping client seeing cached webpage

Ive found this tricky to word so please bear with me.

When I'm updating a website and I ask the client to see the changes, they always see the cached result from last time. Now this isn't a problem as I can ask them to ctrl f5 to refresh all the cache.

My question is, is there a way that I can force the client browser to refresh all of the content automatically? and what is the usual standard on this? I want to still use cache as this speeds up the website, just be able to tell it when it has updated.

I know for javascript files and css files you can change the name of the file so the browser treats it as a new file and re-downloads it e.g. myCssFilev1.2.css but what about the actual HTML?

Upvotes: 1

Views: 157

Answers (4)

kimjxie
kimjxie

Reputation: 801

The HTTP header Last-Modified and Etag use to lessen traffic on the net.

The browser request the web server with Last-Modified header. If the date-time same as server file time, the server just response 304. Or, the time is different, the server response 200 and full content.

The Etag do the same process.

There are more like If-Modified-Since , If-Unmodified-Since, see more on etag, and rfc2616

Upvotes: 0

Doa
Doa

Reputation: 2015

There are a couple of meta tags to do this:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

The cache-control, expires and pragma settings all work in different browsers, so it's best to include all of them. That should solve your problem.

EDIT

As Ariel has noted, you have to be careful with this. If this is a high-load situation, you probably want to find another solution. You can try to find the "middle ground", but tweaking the parameters (setting expires or cache-control's max-age to some value). However, if you don't have an insane server load, then this should be fine.

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146630

It's trickier than it appears. I honestly think that instructing the user to empty his browser's cache is the most straightforward method. Nowadays, hitting Ctrl+Shift+Del is fast and works in most browsers, including Internet Explorer.

Upvotes: 0

Ariel
Ariel

Reputation: 26783

Yes. Say you have a javascript include.

Do:

<SCRIPT src="file.js?<?=filemtime('file.js')?>"></SCRIPT>

What this does is add the file modification time to the url for the file. So when you change the file, to the browser it will look like a new file, and won't be cached.

Be aware that the constant filemtime calls may slow things down.

You can use a combination of dirname() __FILE__ and PHP_SELF to auto calculate the physical file page from the relative one, and then put it in a function so you don't have to type the filename manually twice yourself.

Upvotes: 0

Related Questions