Clear Browser Cache Every Nth Day(s) in PHP

Date Posted: October 9, 2015, 9:05 am


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

  1. Create a cookie variable where we will be storing the current time when user visits the website
  2. Subtract current time to old time then get the result in days
  3. Check if the result is greater or equal to the provided nth day(s) $no_of_days
  4. 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ajax_cache_cleaner = '
<script type = "text/javascript">
 $.ajax({
    url: window.location.href,
    headers: {
        "Pragma": "no-cache",
        "Expires": -1,
        "Cache-Control": "no-cache"
    }
}).done(function () {
    window.location.reload(true);
});
</script>'
;

if( isset( $_COOKIE['clear_browser_cache'] ) ) {
    $old_time = $_COOKIE['clear_browser_cache'];
    $current_time = time();
    $secs = $current_time - $old_time; // seconds between the two times
    $days = $secs / 86400;
    if( $days >= $no_of_days ) {
        setcookie('clear_browser_cache', time(), time() + 86400); // 86400 = 1 day
        echo $ajax_cache_cleaner;
           
    }
} else {
    setcookie('clear_browser_cache', time(), time() + ( $no_of_days * 86400 ) ); // 86400 = 1 day
    echo $ajax_cache_cleaner;
}

Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * Clears the browser cache everyday nth days.
 */

function clear_browser_cache( $no_of_days ) {
   
    $ajax_cache_cleaner = '
    <script type = "text/javascript">
     $.ajax({
        url: window.location.href,
        headers: {
            "Pragma": "no-cache",
            "Expires": -1,
            "Cache-Control": "no-cache"
        }
    }).done(function () {
        window.location.reload(true);
    });
    </script>'
;
   
    if( isset( $_COOKIE['clear_browser_cache'] ) ) {
        $old_time = $_COOKIE['clear_browser_cache'];
        $current_time = time();
        $secs = $current_time - $old_time; // seconds between the two times
        $days = $secs / 86400;
        if( $days >= $no_of_days ) {
            setcookie('clear_browser_cache', time(), time() + 86400); // 86400 = 1 day
            echo $ajax_cache_cleaner;
               
        }
    } else {
        setcookie('clear_browser_cache', time(), time() + ( $no_of_days * 86400 ) ); // 86400 = 1 day
        echo $ajax_cache_cleaner;
    }
   
}

Parameter equals the number of days before executing the cache cleaner script.

Note:

  • Make sure jQuery is loaded first before using the above function.
  • Run the function on the Head part of your HTML document.

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me