dqh1992 2014-03-20 23:25
浏览 159
已采纳

某些html页面没有缓存

I've a code that every time fill in a HTML file with a new content and this file is called on my website by ... but when I change the file content and then go to my page, the page shows the old content and not the new one. I just have 2 pages that I do that, the others are all statics.

How can I just have a "non-cache" for those 2 files?

My code its just like this:

<?php
$fp = fopen('file.html', 'w');
fwrite($fp, $content);
fclose($fp);
?>

and on another file.php I have

<html>
<head>...</head>
<body>
<some html>
<?php include(file.html); ?>
<some html>
</body>
</html>

thanks,

  • 写回答

1条回答 默认 最新

  • duanbohan2015 2014-03-20 23:31
    关注

    Make sure that for that file a header is sent which specifies that it should not be cached; headers like:

    Cache-Control: max-age=0, no-cache, no-store, must-revalidate
    Pragma: no-cache
    Expires: Fri, 20 Mar 2014 00:00:00 GMT
    

    If these files are PHP files you can do this by adding a line like this at the top:

    <?php
    header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: Fri, 20 Mar 2014 00:00:00 GMT');
    ?>
    

    The reason why I used multiple headers here is that there are multiple ways to control the cache; if you really want to make sure it is never cached, you should disable all forms (this way).

    If they are purely HTML files, then you'll need to use some .htaccess magic as described in this answer (add a header to each request using htaccess)

    For clarity, this would be done as follows:

    • Create a .htaccess file in the directory of your file.html
    • Put the following content into this .htaccess file:

    Code:

    <files "file.html">
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "Fri, 20 Mar 2014 00:00:00 GMT"
    </Files>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏