强制浏览器从php重新加载CSS

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个回答

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>
dongyi1939
dongyi1939 静态页面永远不会改变,除非您自己修改代码。 动态页面被编程为根据您的代码自动更改。 想想一个新闻网站:它们需要经常更新:你不能每次都手动更新。 99%的网页都是动态页面,语言不一定是PHP,但它最常见的ID就是这样。
一年多之前 回复
dongluan6784
dongluan6784 是的,你做对了。 PHP代码在“echo auto_version部分”之前也很重要。 它就像一个字典:注意PHP文件中的“function”关键字,{}中的内容是该函数的定义。 因此,当您稍后调用该函数时,您的文件必须知道该函数“意味着什么”。 你可以通过告诉他之前做到这一点。 否则,您将收到错误“未定义auto_version函数”。 是的,如果你要使用任何一点PHP代码,文件扩展名必须是php。 基本上html页面称为“静态页面”,而PHP页面是“动态页面”。
一年多之前 回复
dsfsfsfsfs55656
dsfsfsfsfs55656 如果我做对了,我需要将index.html更改为index.php并摆脱auto_version.php,复制我的新index.php中的所有php代码? 这是否意味着我的所有其他.html文件也必须转换为.php? 这是网站发展的常见情况吗?
一年多之前 回复
doucha4054
doucha4054 好的,所以我没有足够的声誉直接回复你的评论,但你的问题如下:你需要将index.html合并到php文件中。 只需复制index.html的整个html代码并在auto_version.php的php代码之后添加它,现在它将起作用(在?>标记之后)。 您需要加载的页面是auto_version.php页面。 基本上,html文件只能理解html。 PHP文件了解两种HTML和PHP。 如果您使用PHP,则必须将html代码放入PHP文件中。
一年多之前 回复
立即提问