Created
July 11, 2013 00:10
-
-
Save chill117/5971376 to your computer and use it in GitHub Desktop.
Force client browser to download fresh CSS/JS file whenever the file is modified on the server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/* | |
|---------------------------------------------------------------- | |
| Description | |
|---------------------------------------------------------------- | |
Force client browser to download fresh CSS/JS file whenever | |
the file is modified on the server. | |
|---------------------------------------------------------------- | |
| Dependencies | |
|---------------------------------------------------------------- | |
CodeIgniter | |
|---------------------------------------------------------------- | |
| Example Usage | |
|---------------------------------------------------------------- | |
<link rel="stylesheet" href="/css/reset.css<?= cache_buster('/css/reset.css'); ?>" /> | |
OR | |
<script src="/js/modernizr.js<?= cache_buster('/js/modernizr.js'); ?>"></script> | |
*/ | |
function cache_buster($path, $hash_query_string = true) | |
{ | |
$file = BASE_DIR . $path; | |
if (!file_exists($file)) | |
return ''; | |
$mtime = filemtime($file); | |
if ($hash_query_string) | |
$query_string = sha1($mtime); | |
else | |
$query_string = date('Y-m-d', $mtime) . 'T' . date('H:i:s', $mtime); | |
return '?' . $query_string; | |
} | |
/* End of file cache_buster_helper.php */ | |
/* Location: ./application/helpers/cache_buster_helper.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would do it this way, easier to use in the src/href of a link/script tag. Great helper though.