Skip to content

Instantly share code, notes, and snippets.

@chill117
Created July 11, 2013 00:10
Show Gist options
  • Save chill117/5971376 to your computer and use it in GitHub Desktop.
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.
<?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 */
@joshmanders
Copy link

I would do it this way, easier to use in the src/href of a link/script tag. Great helper though.

<?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="<?= cache_buster('/css/reset.css'); ?>" />

        OR

    <script src="<?= 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 $path . '?' . $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