Quick php fix to CSS and Javascript browser cache

I am going to post very handy tip for javascript and css files caching. Browsers automatically cache the css and JS files and when you make change to CSS and JS some times change does not appears.

To prevent this use following

<?php
echo '<link rel="stylesheet" type="text/css" href="style.css?' . filemtime('style.css') . '" />';
?>

And For JS

<?php
echo '<script type="application/javascript"="main.js?' . filemtime('
main.js') . '" />';
?>

The PHP function filemtime returns last modified date of file, so every time you make change in css / JS files the filemtime will return different value and CSS / JS Url will change and browser will refresh CSS / JS file.

😉

One thought on “Quick php fix to CSS and Javascript browser cache

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s