This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cache Control fails
I am currently using:
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
and
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="-1">
<meta http-equiv="pragma" content="no-cache">
Yet it still caches
You can also add Pragma: no-cache. And make sure Last-Modified is far enough in the past: some small clock skews between the client and the server could trick your client into thinking it has a fresh version of the document; use Tue, 15 Nov 1994 12:45:26 GMT for example. And also add an Expires header with date in the past.
I normally add this in my .htaccess
<filesMatch "\.(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>
Modify the fileMatch to add other extentions ...
Add timestamp to the end of each URI, and the browser won't retrieve it from cache (as it will be with another URI on the next load)
Related
So I am making a small web app using php. I'm just inserting into a MYSQL hosted at eHost. http://www.gearedwebdesigns.com/BPHCalcEnter.php
I have zero problems on desktop with caching. Different story on my android Galaxy s7 in Chrome. If I use the chrome inspector and remote devices then I can see the latest php update by doing CTRL+Shift+F5. However, if I refresh by pulling down then it loads an older version. I have tried clearing cache multiple times.
I've added this to my .htaccess
<filesMatch "\.(html|htm|js|php|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>
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
And I have this at the top of my php file.
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
Any ideas? Thanks.
I have an application where a number of otherwise static javascript files are being generated via PHP to allow configuration options to alter the static files (path like: mystaticfile.js.php). Everything works fine EXCEPT that I can't seem to get the cache settings to work and these resources are being reloaded with every page load.
The PHP file uses the following headers to try to set the cache settings:
$expires= 60 * 60 * 24 * 60; //cache for 60 days
header('Pragma: public');
header('Cache-Control: max-age=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
header("content-type: application/x-javascript");
However, when the files are served they're showing headers that look like:
HTTP/1.1 200 OK
Date: Sun, 06 Nov 2016 19:18:00 GMT
Server: Apache/2.2.15 (CentOS)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 744
Keep-Alive: timeout=15, max=95
Connection: Keep-Alive
Content-Type: application/x-javascript
My first thought was that it was that it might be because Apache has the ExpiresActive flag set on but I don't see any ExpiresByType rules set for PHP files.
Reading online it sounds like ETag issues could be the problem, but I've added
Header unset Pragma
FileETag None
Header unset ETag
to the http.conf file (and restarted the service) and still no dice.
Any thoughts?
Source: PHP: Worry about some magical added “Cache-Control” Header ?
These headers are automatically set by the PHP Session module to
prevent browser/proxy based caching of your pages. Depending on your
environment setup, it’s possible to control these headers by using the
session_cache_limiter() function or use the php.ini
To disable these behaviour just pass an empty string to the
session_cache_limiter() function as mentioned in the documentation:
session_cache_limiter('');
I have tried the code below to clear the cache of the index page to show the latest content to the user.
<filesMatch "index.html">
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>
But when I try this code it gives forbidden error.
I'm probably missing something obvious, but my Codeigniter app is not sending headers when I ask it to.
So in any controller or the extended MY_Controller:
$this->output->set_header("Last-Modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT");
$this->output->set_header("Expires: " . gmdate( "D, j M Y H:i:s", time() ) . " GMT");
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
$this->output->set_header("Steve: it aint working you know");
And the headers I get are:
HTTP/1.1 200 OK
Date: Mon, 19 Mar 2012 18:03:06 GMT
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.6
Last-Modified: Mon, 19 Mar 2012 18:03:06 GMT
Expires: Sat, 01 Jan 2000 00:00:01 GMT
Cache-Control: post-check=0, pre-check=0, max-age=0
Pragma: no-cache
Steve: it aint working you know
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 10780
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
So I know it's sending headers from the Steve: header but it's not sending my Cache-Control settings. If I comment out the Cache-Control setting, it still displays the same value.
Where could this be being overridden? Is it Codeigniter, PHP or Apache?
I would check your Apache config, particularly how mod_expires and the Header Directive are configured.
Note that the "header is modified just after the content handler and output filters are run, allowing outgoing headers to be modified." So Apache may be set to modify the headers you've set in PHP.
I'm no Apache expert, but this is what I had to work with to fix a similar issue.
Steps I took:
added the following to my .htaccess file.
<IfModule mod_headers.c>
Header add Cache-Control: "no-store, no-cache, must-revalidate"
</IfModule>
ran
sudo a2enmod headers
sudo service apache2 restart
For a small intranet site, I have a dynamic (includes AJAX) page that is being cached incorrectly by Firefox. Is there a way to disable browser caching for a single page?
Here's the setup I'm using:
Apache under XAMPP, running on a Windows server
PHP
Clarification
The content that I'm primarily concerned about is page text and the default options in some <select>s. So I can't just add random numbers to the end of some image URLs, for example.
Update:
I followed the suggestions I've gotten so far:
I'm sending nocache headers (see below)
I'm including a timestamp URL parameter and redirecting to a new one if the page is reloaded after 2 seconds, like this:
$timestamp = $_GET['timestamp'];
if ((time()-$timestamp) > 2) {
header('Location:/intranet/admin/manage_skus.php?timestamp='.time());
}
Now Firebug shows that the headers specify no cache, but the problem persists. Here are the response headers for the page:
Date Fri, 25 Sep 2009 20:41:43 GMT
Server Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
X-Powered-By PHP/5.2.8
Expires Mon, 20 Dec 1998 01:00:00 GMT
Last-Modified Fri, 25 Sep 2009 20:41:43 GMT
Cache-Control no-cache, must-revalidate
Pragma no-cache
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html
Add current timestamp as parameter of url, e.g.
http://server.com/index.php?timestamp=125656789
I think this tells you what you want:
http://www.thesitewizard.com/archive/phptutorial2.shtml
Look for "Preventing the Browser From Caching"
header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );
You should send the following header:
Cache-control: no-cache
in the HTTP response.
You could add these headers:
Cache-Control: no-cache
And (for backward compatibility with HTTP/1.0 clients)
Pragma: no-cache
Here's another take that isn't PHP specific.
Try this in your <head> </head> section:
<meta http-equiv="cache-control" content="no-cache, no store"/>
<meta http-equiv="Expires" Content="Mon, 25 May 2009 19:07:03 GMT">
Found that at the end of a long thread here:
http://forums.mozillazine.org/viewtopic.php?f=25&t=673135&start=75
Use the header() function. You have to set a few to cover all browsers; see http://www.php.net/manual/en/function.header.php#75507