<link href="/stylesheet.css?<?php echo time(); ?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/site-script.js?<?php echo time(); ?>"></script>
// OR
<link href="/stylesheet.css?<?php echo filemtime('mycss.css'); ?>" rel="stylesheet" type="text/css" />
// OR
/**
* Given a file, /css/base.css, replaces it with a string containing the
* file's mtime, /css/base.1221534296.css.
*
* @param $file The file to be loaded. Must be an absolute path
*/
function auto_version($file)
{
if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
return $file;
$mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}
<link rel="stylesheet" href="/css/base.css" type="text/css" />
// OR
// OR
/**
* Given a file, /css/base.css, replaces it with a string containing the
* file's mtime, /css/base.1221534296.css.
*
* @param $file The file to be loaded. Must be an absolute path
*/
function auto_version($file)
{
if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
return $file;
$mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}
Using javascript
Using .htaccess
FileETag None
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"
Using GIT
/**
* Get the hash of the current git HEAD
* @param str $branch The git branch to check
* @return mixed Either the hash or a boolean false
*/
public static function get_commit( $branch='master' ) {
if ( $hash = file_get_contents( sprintf( '.git/refs/heads/%s', $branch ) ) ) {
return $hash;
} else {
return time();
}
}