dousaoxiancy199896 2018-02-26 21:26
浏览 53
已采纳

PHP缓存包括css

Im using this code for caching my data:

<?php
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.php';
$cachetime = 3600;

if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
  include($cachefile); //include cache file
}else{
  ob_start(); //start output buffering
?>

<?php

....

echo '<div class="profile-font2">', $items, '</div>';

   $fp = fopen($cachefile, 'w'); //open cache file
  fwrite($fp, ob_get_contents()); //create new cache file
  fclose($fp); //close cache file
  ob_end_flush(); //flush output buffered
}
?>

The echo output get stored in the file. But I´ve a problem with my styles. The <div class=""> or table class gets ignored. So I just get the naked data. How is it possible that my styles get considered?

  • 写回答

1条回答 默认 最新

  • dongye9228 2018-02-26 21:37
    关注

    echo '<div class="profile-font2">' - here you are just creating a div - it has a class-attribute, yes - but since you don't include any stylesheet - that means the class-attribute is worthless.

    So, it depends on what you want to achive:

    • do you want to render the (cached) content according to the latest stylesheet?
    • do you want to cache the style-sheet as well?

    For the first example, it would be quite easy, cause you just need to include the actual style-sheet before printing the cached content, i.e.:

    <head>
    <link rel="stylesheet" type="text/css" href="mystyle.css">
    </head>
    <body>
    <?include("cachedContent.extension")?>
    </body>
    

    The second example would be more tricky, cause you need to "align" all the cached style-sheets with their respective HTML-File. I don't think that's trivial and hence don't have an answer for that case.

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

报告相同问题?

悬赏问题

  • ¥15 怎么做商品窗体,完完全全不会
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 铜与钢双金属板叠加在一起每种材料300mm长,18mm宽,4mm厚一端固定并加热至80℃,当加热端温度保持不变时另一端的稳态温度。ansys
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥15 ANSYS分析简单钎焊问题
  • ¥20 bash代码推送不上去 git fetch origin master #失败了