I have a php site running in cloud server.When ever i add new files css, js or images the browser is loading the same old js, css and image files stored in cache.
My site has a doctype and meta tag as below
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Page-Enter" content="blendTrans(Duration=1.0)">
<meta http-equiv="Page-Exit" content="blendTrans(Duration=1.0)">
<meta http-equiv="Site-Enter" content="blendTrans(Duration=1.0)">
<meta http-equiv="Site-Exit" content="blendTrans(Duration=1.0)">
Because of the above doctype and meta code am i loading the same files cached in browser instead of new one
No Cache in all Browsers
. You can also do a ?randomGeneratedNumber on the files you dont want to be cached.
try this
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
Here, if you want to control it through HTML: do like below Option 1:
<meta http-equiv="expires" content="Sun, 01 Jan 2014 00:00:00 GMT"/>
<meta http-equiv="pragma" content="no-cache" />
And if you want to control it through PHP: do it like below Option 2:
header('Expires: Sun, 01 Jan 2014 00:00:00 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 Option 2 IS ALWAYS BETTER in order to avoid proxy based caching issue.
You can try this:
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Connection: close");
Hopefully it will help prevent Cache, if any!
I had problem with caching my css files. Setting headers in PHP didn't help me (perhaps because the headers would need to be set in the stylesheet file instead of the page linking to it?).
I found the solution on this page: https://css-tricks.com/can-we-prevent-css-caching/
The solution:
Append timestamp as the query part of the URI for the linked file. (Can be used for css, js, images etc.)
For development:
<link rel="stylesheet" href="style.css?<?php echo date('Y-m-d_H:i:s'); ?>">
For production (where caching is mostly a good thing):
<link rel="stylesheet" type="text/css" href="style.css?version=3.2">
(and rewrite manually when it is required)
Or combination of these two:
<?php
define( "DEBUGGING", true ); // or false in production enviroment
?>
<!-- ... -->
<link rel="stylesheet" type="text/css" href="style.css?version=3.2<?php echo (DEBUGGING) ? date('_Y-m-d_H:i:s') : ""; ?>">
EDIT:
Or prettier combination of those two:
<?php
// Init
define( "DEBUGGING", true ); // or false in production enviroment
// Functions
function get_cache_prevent_string( $always = false ) {
return (DEBUGGING || $always) ? date('_Y-m-d_H:i:s') : "";
}
?>
<!-- ... -->
<link rel="stylesheet" type="text/css" href="style.css?version=3.2<?php echo get_cache_prevent_string(); ?>">
Prevent browser cache is not a good idea depending on the case. Looking for a solution I found solutions like this:
<link rel="stylesheet" type="text/css" href="meu.css?v=<?=filemtime($file);?>">
the problem here is that if the file is overwritten during an update on the server, which is my scenario, the cache is ignored because timestamp is modified even the content of the file is the same.
I use this solution to force browser to download assets only if its content is modified:
<link rel="stylesheet" type="text/css" href="meu.css?v=<?=hash_file('md5', $file);?>">
stat
call. Without filesystem cache, 16ns, tops? With cache, reliably < 8ns. Nanoseconds. And on my system MD5 can process 754 MiB/s without blinking. (openssl speed md5
) Combined, a 100KB CSS file would have a combined additional overhead of… 129µs (microseconds, 0.1295ms) + 8ns (which does not meaningfully contribute to the final number) = 129µs.
hash_file('md5', $deployment_counter)
or hash_file('md5', $cache_clear_counter)
are the first that come to mind.
You can also use query string to your cached files. It will not affect to neither behaviour of your style nor js files.
For example:
example.com/mystyle.css ->
example.com/mystyle.css?<?php echo random(1000, 5000); ?>
Success story sharing
session_start()
afterwards, it will overwrite your header withCache-Control: private, max-age=10800, pre-check=10800
because 180 minutes is the default value ofsession.cache_expire
. If you can not avoid starting the session, but you need to disable the cache usesession_cache_limiter('private');session_cache_expire(0);
.header
function is a boolean for replace. The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type.