# Fix for stubborn browsers cache and css files

**URL:** <https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400>\
**Category:** Community Guides\
**Created:** [February 5, 2021, 7:22am UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400 "2021-02-05T07:22:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 7:22am UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/1 "2021-02-05T07:22:44Z")

</div>

I’ve found that browsers have a habit of keeping CSS files in the cache which is a problem when developing sites.

**This is also an issue for anyone viewing your site as any changes made to the CSS will not be shown in the viewer’s browser.**

I have found a way to allow changes to CSS files to instantly update in the browser when a page is loaded or refreshed without the need to clear the cache.

This fix uses one line of PHP code and so requires that your page has the `.php` file extension.

This is a typical line in the head section to set the stylesheet:

```
<link rel='stylesheet' type='text/css' href='/path/style.css' />

```

Replace that with this:

```
<link rel='stylesheet' type='text/css' href='/style.css?<?php echo filemtime($_SERVER["DOCUMENT_ROOT"] . "/path/style.css"); ?>' />

```

So what does this do?

This PHP code appends the creation time of the file to the end of the file name in the form `style.css?681239872`, the numeric value will change when you make a change to `style.css`.

This tricks the browsers into thinking something is new so it’ll reload the `style.css` file with the new changes and caches it until the next change.

This has saved me a lot of headaches and time while developing my site.

I share it with you and hope you find it useful.

Thanks.

---

<div class="post-metadata">

**Author:** ![Oxy](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/oxy/32/37989_2.png) [@Oxy](https://forum.infinityfree.com/u/Oxy)\
**Post date:** [February 5, 2021, 11:48am UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/2 "2021-02-05T11:48:12Z")

</div>

I don’t want to spoil your topic  
but in the options of dev tools in FF  
you can easily disable the cache

 ![image](https://forum-cdn.infinityfree.net/original/2X/1/150ab080ad659ead89841b6a5a388736ca3349aa.png)

---

<div class="post-metadata">

**Author:** ![FlutterHoney](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/flutterhoney/32/17560_2.png) [@FlutterHoney](https://forum.infinityfree.com/u/FlutterHoney)\
**Post date:** [February 5, 2021, 11:49am UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/3 "2021-02-05T11:49:25Z")

</div>

_when toolbox is open_  
Does that mean only if DevTools is opened?

---

<div class="post-metadata">

**Author:** ![Oxy](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/oxy/32/37989_2.png) [@Oxy](https://forum.infinityfree.com/u/Oxy)\
**Post date:** [February 5, 2021, 11:53am UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/4 "2021-02-05T11:53:07Z")

</div>

it should not be a problem because you keep it open during development anyway

![image](https://forum-cdn.infinityfree.net/original/2X/1/1cc8552dce3a3d92acbd4fd9c6cd35214155f4c8.png)

there is also an option to permanently turn off the cache on the disk as well as in the RAM  
inside the config of FF

There is also a developer version of the browser

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 1:09pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/5 "2021-02-05T13:09:32Z")

</div>

i thought this was more useful because people viewing your site also have the same issue of cached css files and wont normally see the change you make to your site

but with this little php any changes made to css are reloaded instantly by the viewers browser as soon as they load any page

---

<div class="post-metadata">

**Author:** ![Erraticstuff](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/erraticstuff/32/56349_2.png) [@Erraticstuff](https://forum.infinityfree.com/u/Erraticstuff)\
**Post date:** [February 5, 2021, 1:24pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/6 "2021-02-05T13:24:58Z")

</div>

You can use `time()` in PHP it returns unix time in numbers

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 1:29pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/7 "2021-02-05T13:29:33Z")

</div>

[Erraticstuff](https://forum.infinityfree.com/u/Erraticstuff) You can use `time()` in PHP it returns unix time in numbers

if I had used time() it would work but the time would change on every page load and the browser would download the style.css every time

Im using filemtime() because I want to get the timestamp of the css file for when it was last updated, that way when you update the file the browser sees it as a new file and reloads the style.css file and caches it until you change it again

doing it this way the browser only reloads style.css on change which will save on download times and bandwidth  
---------------- edit ---------------

sorry i could be wrong about time() i might be thinking of date() lol

---

<div class="post-metadata">

**Author:** ![Admin](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/admin/32/36437_2.png) [@Admin](https://forum.infinityfree.com/u/Admin)\
**Post date:** [February 5, 2021, 3:10pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/8 "2021-02-05T15:10:04Z")

</div>

This is actually quite a nice way to bust caches, while still allowing browsers to cache files as long as they don’t change! It doesn’t disable cache entirely, so you still get all the benefits of caching, but any changes is also visible immediately.

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 3:13pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/9 "2021-02-05T15:13:52Z")

</div>

thanks admin 🙂

---

<div class="post-metadata">

**Author:** ![Oxy](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/oxy/32/37989_2.png) [@Oxy](https://forum.infinityfree.com/u/Oxy)\
**Post date:** [February 5, 2021, 3:49pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/10 "2021-02-05T15:49:55Z")

</div>

> [@lovebug](#):
>
> because people viewing your site also have the same issue of cached css files and wont normally see the change you make to your site

Of course, but to change the extensions of each html document is not desirable to me  
because I try to have static resources wherever possible ( [EP limits](https://support.infinityfree.net/limits/entry-process-limit/#how-do-i-reduce-my-entry-process-usage) )  
and it’s easier to run/test because I don’t need to turn on the local web server.

It’s still easy when someone has one CSS file, but I often see that many have more than 10 😄

If you frequently make changes to CSS and other resources  
you can also play with the Cache-Control settings and reduce the values from various parameters (how many seconds will hold something in the cache) or ETag…

> **[Cache-Control header - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control)**
>
> The HTTP Cache-Control header holds directives (instructions) in both requests and responses that control caching in browsers and shared caches (e.g., Proxies, CDNs).

> **[ETag header - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag)**
>
> The HTTP ETag (entity tag) response header is an identifier for a specific version of a resource.
> It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content has not...

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 3:54pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/11 "2021-02-05T15:54:31Z")

</div>

I see your point , it would be a pain changing a lot of html files containing many css files

ln my case my website has only a single index.php that loads the css followed by loading the page content files into the body so there was only 1 file that needed to be edited as the pages are just content and have no head or body section

---

<div class="post-metadata">

**Author:** ![ManuTheCoder](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/manuthecoder/32/20353_2.png) [@ManuTheCoder](https://forum.infinityfree.com/u/ManuTheCoder)\
**Post date:** [February 5, 2021, 5:36pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/12 "2021-02-05T17:36:37Z")

</div>

Did you know that you can hit CTRL + SHIFT + R to **hard reload the page**?  
Or you can just use the .htaccess file

```auto
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

```

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 5:40pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/13 "2021-02-05T17:40:57Z")

</div>

[ManuTheCoder](https://forum.infinityfree.com/u/ManuTheCoder)

I was doing that all the time but browsers are often stubborn and refuse to load the changed css even after a hard refresh !, doing it this way with php guarantees it’ll load without having to hard refresh which is good for anyone visiting the site as a lot of people dont even know about hard refresh

---

<div class="post-metadata">

**Author:** ![ManuTheCoder](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/manuthecoder/32/20353_2.png) [@ManuTheCoder](https://forum.infinityfree.com/u/ManuTheCoder)\
**Post date:** [February 5, 2021, 6:39pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/14 "2021-02-05T18:39:48Z")

</div>

Or you can purge cache if you use cloudflare this helps a lot

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 6:42pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/15 "2021-02-05T18:42:01Z")

</div>

well I was doing all of that before but with this php you dont need to purge any caches including cloudflare, it just saves a lot of hassle

this just helps if youre making changes to css a lot and trying things out, if youre changing site content like images etc then obviously the cache will still need purging

----- edit ---- I just tried the php on images and it does’nt work , oh well I still have to clear the cache when changing images lol

---

<div class="post-metadata">

**Author:** ![HTMLJSCSS](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/htmljscss/32/13091_2.png) [@HTMLJSCSS](https://forum.infinityfree.com/u/HTMLJSCSS)\
**Post date:** [February 5, 2021, 8:25pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/17 "2021-02-05T20:25:01Z")

</div>

You can also use this:

```auto
<link rel="stylesheet" href="<?php echo rand(1, 999); ?>">

```

or this…

```auto
<script type="text/javascript" src="<?php echo time() - rand(1, 999); ?>"></script>

```

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 8:46pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/18 "2021-02-05T20:46:46Z")

</div>

@ManuTheCoder thanks for that info, btw this is the wrong thread !

---

<div class="post-metadata">

**Author:** ![ManuTheCoder](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/manuthecoder/32/20353_2.png) [@ManuTheCoder](https://forum.infinityfree.com/u/ManuTheCoder)\
**Post date:** [February 5, 2021, 8:59pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/19 "2021-02-05T20:59:40Z")

</div>

Oh, I’m so sorry… ☹

---

<div class="post-metadata">

**Author:** ![lovebug](https://forum.infinityfree.com/user_avatar/forum.infinityfree.com/lovebug/32/57624_2.png) [@lovebug](https://forum.infinityfree.com/u/lovebug)\
**Post date:** [February 5, 2021, 9:35pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/20 "2021-02-05T21:35:15Z")

</div>

no worries 🙂

---

<div class="post-metadata">

**Author:** ![KeKa000](https://forum.infinityfree.com/letter_avatar/keka000/32/6_449c533f586939b6e5f246632a46421f.png) [@KeKa000](https://forum.infinityfree.com/u/KeKa000)\
**Post date:** [August 8, 2021, 1:15pm UTC](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400/21 "2021-08-08T13:15:04Z")

</div>

I did not try ur solution this yet.  
But if it works, it is actually giving the perfect result.  
Benefits in more speed site from keeping caching  
+  
It makes site up to date always

[Next page](https://forum.infinityfree.com/t/fix-for-stubborn-browsers-cache-and-css-files/41400.md?page=2)
