2

I need to prevent caching of a page, to ensure that it always displays the latest data. I have done this by adding some PHP at the top of the page.

//Set no caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

It has overcome the immediate problem, but I am not sure if this means that external scripts are also having to be reloaded every time. The page uses jquery, jquery-ui and some other scripts so it would be nice to avoid reloading them for the benefit of users with a slow connection.

Do those headers force everything to reload or just the actual code on the page?

TrapezeArtist
  • 777
  • 1
  • 13
  • 38
  • Its not reload static page. Its only load php page. If u want cache static file like js, css. U could add headers in apache or web server to avoid reload static file – Ganesan Karuppasamy Jul 16 '15 at 11:50

1 Answers1

0

Headers only apply to the file you add them to. This is partly for security (files on your website might be coming from several different servers) and also because you might well want different cache settings for different objects.

If you're loading jQuery via a CDN, it'll use the CDN's cache settings, which will invariably have a very long expiry time. (The CDN might well use gzip compression too.)

For most PHP sites, everything other than the PHP pages will be served directly via the server without touching the PHP interpreter, so for example in Apache, you can use mod_expires to control cache settings for files of a certain content-type (e.g. image/jpg or application/javascript).

To verify what's going on, in your browser, if you open the developer tools and look at the Network panel, you'll be able to inspect the cache settings for every object on the page.

If you want to guarantee the new version of something - e.g. a CSS file you've just modified - is sent to the user, a quick and easy way is to add a version number to the query string, then the browser will always reload it.

Community
  • 1
  • 1
William Turrell
  • 3,227
  • 7
  • 39
  • 57
  • That's what I was hoping to hear. Also a very clear explanation, thank you. The last para is also a useful piece of advice which I will stash away for use at the appropriate moment. – TrapezeArtist Jul 16 '15 at 12:05