Question

I've a code that every time fill in a HTML file with a new content and this file is called on my website by ... but when I change the file content and then go to my page, the page shows the old content and not the new one. I just have 2 pages that I do that, the others are all statics.

How can I just have a "non-cache" for those 2 files?

My code its just like this:

<?php
$fp = fopen('file.html', 'w');
fwrite($fp, $content);
fclose($fp);
?>

and on another file.php I have

<html>
<head>...</head>
<body>
<some html>
<?php include(file.html); ?>
<some html>
</body>
</html>

thanks,

Was it helpful?

Solution

Make sure that for that file a header is sent which specifies that it should not be cached; headers like:

Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Fri, 20 Mar 2014 00:00:00 GMT

If these files are PHP files you can do this by adding a line like this at the top:

<?php
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: Fri, 20 Mar 2014 00:00:00 GMT');
?>

The reason why I used multiple headers here is that there are multiple ways to control the cache; if you really want to make sure it is never cached, you should disable all forms (this way).

If they are purely HTML files, then you'll need to use some .htaccess magic as described in this answer (add a header to each request using htaccess)

For clarity, this would be done as follows:

  • Create a .htaccess file in the directory of your file.html
  • Put the following content into this .htaccess file:

Code:

<files "file.html">
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Fri, 20 Mar 2014 00:00:00 GMT"
</Files>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top