0

So i found How to force the browser to reload cached CSS/JS files? question there, but i have troubles figuring out what i am doing wrong with php function written in the answers.

<? php
/**
 *  Given a file, i.e. /css/base.css, replaces it with a string containing the
 *  file's mtime, i.e. /css/base.1221534296.css.
 *  
 *  @param $file  The file to be loaded.  Must be an absolute path (i.e.
 *                starting with slash).
 */
function auto_version($file)
{
  if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
    return $file;

  $mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
  return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}

I tried to copy-paste all of this to my website, changed .htaccess, created php file, made a proper link to php echo main.css in my project, did everything like it was suggested in the answer. But my .css file failed to load, console showed error about my php query failure. My guess is the code above must be configured in some way, but i have almost none of the php knowledge and this part about @param $file makes me wonder, how must i write this? @param $file = /css/main.css or what does it mean? Or i need to place /css/main.css in place of every $file? I cant comment answer in that original thread, so created a new one

edit: structure of my site is this:

index.html
auto_version.php
css/main.css

to index.html i added:

<link rel="stylesheet" href="<?php echo auto_version('/css/main.css'); ?>" type="text/css" />

I also edited first block of code with part, as it stands in mine .php file. So as you can see, index.html and auto_version.php both located in root folder of website, and main.css is inside of css folder. May this be an issue im not sure.

1 Answers1

0

Did you put this in your html code?

    <?php 
function auto_version($file)
{
  if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
    return $file;

  $mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
  return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}
?>

<link rel="stylesheet" href="<?php echo auto_version('/css/main.css'); ?>" type="text/css" />

Its important you put the function code before you call it in the html code

Also make sure that your file has a .php extension.

If you did, or that didnt work, then i suspect that your path is wrong. Go on Filezilla or whatever youre using to manage your website files, and from the first folder, go to your css file. Then take that path.

For example, the path for my boostrap css file on my linux server is "/var/www/html/bootstrap/css/bootstrap-theme.css" You must start from the very first parent file (which is "/", the root of your website). Its normal if your path doesnt look like mine, its completely arbitrory.

Then just replace the old path with the new path u figured out

<link rel="stylesheet" href="<?php echo auto_version('/var/www/html/bootstrap/css/bootstrap-theme.css'); ?>" type="text/css" />

If that doesnt work, show me your PHP error message please

EDIT : this is how your auto_version.php file should look

<?php 
function auto_version($file)
{
  if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
    return $file;

  $mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
  return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}
?>

<doctype html> 
etc
<link rel="stylesheet" href="<?php echo auto_version('/css/main.css'); ?>" type="text/css" />
etc
<body>
etc
</body>
<html>
Benoit F
  • 479
  • 2
  • 10
  • Okay so i dont have enough reputation to reply to your comment directly but your issue is the following : you need to merge the index.html into the php file. Just copy the entire html code of index.html and add it after the php code of auto_version.php and now it will work (after the ?> tag). And the page you need to load is the auto_version.php page. Basically, html files only understand html. PHP files understand BOTH html and php. If youre using PHP you must put the html code into a PHP file. – Benoit F Jul 09 '19 at 13:33
  • If i got it right, i need to change index.html to index.php and get rid of auto_version.php, copying all the php code in my new index.php? Does this mean all my other .html files must be converted to .php too? Is this common situation for website developing? – Battletron Jul 09 '19 at 13:40
  • Yes you got it right. Also its important that the PHP code is before the "echo auto_version part". Its like a dictionary : notice the "function" keyword in the PHP file, whats inside the {} is the definition of that function. So when you call that function later, your file must know what that function "means". And you can do that by telling him before. Otherwise you would get an error "auto_version function is not defined". And yes if youre gonna use any bit of PHP code the file extension must be php. Basically html pages are called "static pages" while PHP pages are "dynamic pages". – Benoit F Jul 09 '19 at 13:53
  • Static pages never change, unless you modifiy the code yourself. Dynamic pages are programmed to change automatically based on your code. Think of a news website : they need to be updated extremely often : you cant do that manually everytime. 99% of web pages are dynamic pages, the language isnt necessarily PHP but its the most common id say. – Benoit F Jul 09 '19 at 13:55