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 !