cowgirl
cowgirl

Reputation: 51

How to control browser cache?

I'm working on a website, where the audience isn't very comfortable with computers. A complaint received often in the past was about (minor) new updates, though important ones not being visible (like date and times being changed). Problem: browser caching and people not knowing how to refresh a browser. Because of the rather large audience, it will be impossible explaining that to all of them.

We'll be putting an entire new site online very soon, so I suspect that this change will cause the browser to get the update...am I right on this one?

But in order to prevent the complaints of the past I would like a better control of the user's browser caching.

It would be best to put a caching time (between a day and one or two weeks) per page, because for the general info caching is a good thing, because it isn't updated that often and it isn't as sensitive to time as our calender is! But I want a new version of the calender within a day from the last visit. --> I would even consider caching parts of pages if possible (header and footer are less likely to change then content etc.)

What's the best approach? Should I change something on the server settings (as far as the hosting allows me to) and how can I do this (hosting control panel, php.ini)?

Or should I do something in PHP? Or in the HTML like I found over here: HTML Cache control

Or maybe a combination of some/all of the above?

Upvotes: 0

Views: 683

Answers (2)

saqibahmad
saqibahmad

Reputation: 962

You can achieve this by .htaccess:

<filesMatch "\.(php|html|htm|js|css)$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</filesMatch>

This will expire the server cache and each request will get the new pages

Upvotes: 0

unixmiah
unixmiah

Reputation: 3143

You can either do this with PHP or HTML

PHP

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

HTML

<meta http-equiv="expires" content="Day, 01 Mon 2014 00:00:00 GMT"/>
<meta http-equiv="pragma" content="no-cache" />

make sure to put that in your main include file for php or html to do this for all pages. I would use the php header over html.

Upvotes: 3

Related Questions