Website does not update in real time, cache problem

Asked

Viewed 6,623 times

0

My website does not update at the same time that I modify a file, it takes a long time for modifications to appear, I have tried by means of META TAGS, file. manifest, and Pragma in PHP. Unsuccessful.

Just when I add "? v=2" for example, it updates, I did it dynamically, because if I update the file, the "? v=2" is already outdated and I have to use "? v=3". It doesn’t happen to all the web sites.

PHP 5.4.x LINUX server.

  • Sorry, but I didn’t get it right, does the cache happen in the php file? Or in javascript? In which of the files are you modifying. If it is php, which framework are you using? Because who stores cache is the browser or the server, in the case of the server is either a config of the framework or is in the hosting settings.

  • Another chance, where are you testing? College, work? The company does not have its own cache system?

  • I changed server (Locaweb) and the problem persisted, however, I created another index.php with the same code, and this stopped caching, I no longer need to use ? v=X. Very strange. All indications were problem in the file. I will continue the tests.

  • Good evening Fabio, the answer solved the problem?

  • Managed to solve your problem?

  • Got, just changing server even, is some cache configuration that even the support of the company can not cure me the problem.

Show 1 more comment

2 answers

1


If the pages you refer to are PHP files they usually don’t cache (ranging from browser to browser), to avoid caching create a file called global.php and add the following content:

<?php
$g = gmdate('D, d M Y H:i:s');
header('Expires: ' . $g . ' GMT');
header('Last-Modified: ' . $g . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

And use include to add this file to all your required PHP files, for example on index.php

<?php
include 'global.php';

...Resto arquivo...

Pages that are already cached will continue until they expire, but once they expire they will never be cached.

But I’ll be honest, if they are PHP files you are very likely already using "headers" to cache, maybe you are using some framework and this has no way to respond.

Another possibility is that there is some file .htaccess that’s causing this.

If the above code I quoted does not work please edit your question and add details. I hope it helps.

If static files, such as .js, .css, .html, .jpg, etc. you can use .htaccess to prevent caching (although I don’t recommend it, because static files rarely have modifications and the best would be to cache them). Example (Soen):

<filesMatch "\.(?i:html|htm)$">
  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>

In this example only the file .html and .htm will not be cached.

Reported: It is possible to use if-modified-Since with "304 not modified" without PHP

  • 1

    Friend, I really liked your answer, I will try this alternative and put here the result. I thought I had solved it, but actually the problem persists. Thank you very much.

  • @Fabioweydson you tested to open the browser anonymously and check if the cache is occurring?

0

try using this script was with a similar problem and solved

<script>
$(document).ready(function(){
setInterval(function(){cache_clear()},3000);
});
function cache_clear()
{
window.location.reload(true);
}
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.