duangu9173 2019-07-09 05:01
浏览 113
已采纳

强制浏览器从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.

  1. <? php
  2. /**
  3. * Given a file, i.e. /css/base.css, replaces it with a string containing the
  4. * file's mtime, i.e. /css/base.1221534296.css.
  5. *
  6. * @param $file The file to be loaded. Must be an absolute path (i.e.
  7. * starting with slash).
  8. */
  9. function auto_version($file)
  10. {
  11. if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file))
  12. return $file;
  13. $mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
  14. return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
  15. }

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:

  1. index.html
  2. auto_version.php
  3. 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条回答 默认 最新

  • duandong9195 2019-07-09 05:21
    关注

    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>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥99 关于#javascript#的问题:怎么样写一个浏览器插件的js,填充浏览器元素:
  • ¥15 关于#hadoop#的问题:按照老师上课讲的步骤写的
  • ¥20 有人会用这个工具箱吗 付fei咨询
  • ¥30 成都市武侯区住宅小区兴趣点
  • ¥15 Windows软实时
  • ¥15 自有服务器搭建网络隧道并且负载均衡
  • ¥15 opencv打开dataloader显示为nonetype
  • ¥15 MacOS 80端口外网无法访问
  • ¥50 js逆转反解密-会的来
  • ¥15 wrodpress如何调取数据库并展示
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部