Carlo Fontanos

Full Stack Developer

Clear Browser Cache Every Nth Day(s) in PHP

This code is useful when you are making a lot of changes to your styles and js scripts and you want your visitors / users to see the updated version page and not the old cached version.

The Process

Create a cookie variable where we will be storing the current time when user visits the website

Subtract current time to old time then get the result in days

Check if the result is greater or equal to the provided nth day(s) $no_of_days

If greater, execute our AJAX cache cleaner script with the help of location.reload(true) then set the cookie variable to the current time. Setting the parameter to true will prevent the browser from fetching the cached version of the page.