-1

I would like, when someone is visiting my website, to make sure the browser has the last version of my files CSS, JS, etc. The idea I came with is to add an extra reference on my links:

Instead of using

<script type="text/javascript" src="www.mywebsite.com/my_file.js"><script>

I'm calling

<script type="text/javascript" src="www.mywebsite.com/my_file.js?lastUpdate"><script>

And i manage to replace lastUpdate by the date of the last modification of the file. The link becomes "new" and the browser is updating his cache regarding the file. Note that all my files are on my server.

To do so, I'm calling a php function (summarised)

function getLastUpdate($link){
    if(file_exists($link)){
        return $link.'?'.filetime($link);
    }
    return $link;
}

and call it :

echo '<script type="text/javascript" src="'.getLastUpdate('www.mywebsite.com/my_file.js').'"><script>';

Everything works. Almost. The function is stored in ./code/function.php And it is used by many files in many different folders. My problem : I don't know what to put for $link. Regarding the url address, it sometimes working, sometimes not. If I give absolute file-path (would be easier for me), the function file_exists returns false. If I give a relative path, it sometimes work, sometimes not. I guess due to the fact that I'm using an url-rewriting, and the link is changing ?

What am I doing wrong ? How should I solve this ? Thanks !

Takit Isy
  • 9,688
  • 3
  • 23
  • 47
Fenix Aoras
  • 221
  • 3
  • 10
  • I'm explaining here the problem I'm facing, how I'm trying to solve it. I'm open to any other way to achieve it... Such as change headers of my files ? – Fenix Aoras Jul 11 '19 at 15:38

1 Answers1

0

I'm using that kind of code to always get the last version :

Function:

function file_versioning($file){
    $filemtime = filemtime($file);
    return "{$file}?v={$filemtime}";
}

Call of the function in "Core.php":

$CssLib = file_versioning("{$IncDir}/Lib.css");
// $IncDir is the relative path from the php script to the "library folder"
echo <<<EOD
    <link rel="stylesheet" type="text/css" href="{$CssLib}" />
EOD;

Call of the "Core" in "index.php":

$IncDir ='.inc/Libs';
require $IncDir.'/Core.php';

⋅ ⋅ ⋅

I don't know if that's the best way to do it… But well, that works. I can have multiple calls of the core at different places, but the relative path used by that core is written in each call script.

halfer
  • 19,824
  • 17
  • 99
  • 186
Takit Isy
  • 9,688
  • 3
  • 23
  • 47
  • This is similar to my code, we are on the same idea. But what you give as $file ? absolute, relative, etc. ? This function will return error if the file is not found – Fenix Aoras Jul 11 '19 at 15:41
  • 1
    @FenixAoras I was working on it but wanted to post an answer before your topic got closed. I've updated ! Would that kind of things work for you ? – Takit Isy Jul 11 '19 at 15:54
  • I tried to convert all my links to make them start by the root like this > ./code/script.js However filetime sometimes throw an error "stat failed for filelink..." – Fenix Aoras Jul 11 '19 at 15:59
  • @FenixAoras Maybe that is why I used relative paths… :) (I don't remember exactly, it's been a while !) – Takit Isy Jul 11 '19 at 16:01