-1

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,

3
  • depends on what is caching it. Are you using a server-side cache? Or relying on the browser?
    – Tularis
    Commented Mar 20, 2014 at 23:26
  • relying on the browser. I have a linux with apache running on it.
    – dpedoneze
    Commented Mar 20, 2014 at 23:27
  • How are you caching? Can you post some code for us to review?
    – larsAnders
    Commented Mar 20, 2014 at 23:42

1 Answer 1

1

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>
5
  • The thing is, i'm generating the .html reading the data and putting it on a $var and after writing it on a file.html using fwrite($file,$var); if I set the header using PHP i'd be setting the filewritter.php headers and not the file.html. I saw the htaccess but it just have ExpireByType html/css that is used for every html file and not just those 2 files that I'm looking for Cheers
    – dpedoneze
    Commented Mar 21, 2014 at 0:23
  • you can always limit it to only a single (or a couple) of files by using <Files "x.html"> Header add ... </Files> as explained in the Apache Documentation
    – Tularis
    Commented Mar 21, 2014 at 0:26
  • Yes, but It don't have just "Expire", It has ExpireDefault and ExpireByType link Or if I just put <Files "file.html"> ExpiresDefault "access plus 6 hours" </Files> will it get the default expire just for the file? If I just add ExpiresByType text/html "modification plus 6 hours" and I modify (by fwrite) will it after 6 hours expires the cache?
    – dpedoneze
    Commented Mar 21, 2014 at 0:38
  • Thanks @Tularis I actually didn't do as you said but I did this and it worked ExpiresActive On ExpiresByType text/html M0 it expires all the html since they are modified, exactly what I was looking for Cheers
    – dpedoneze
    Commented Mar 21, 2014 at 0:45
  • yes it would, remember though that "Expire" is a header, while ExpireDefault is a setting which then auto-generates an expire-header. Using the method I posted the expiration-date is in the past, so it will always not-cache the document.
    – Tularis
    Commented Mar 21, 2014 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.