【问题标题】:A way to "No cache" with Php on part of my page?在我的部分页面上使用 Php“无缓存”的方法?
【发布时间】:2012-09-22 04:19:29
【问题描述】:

我的网站页面上有一个表单。我有一个从提交表单回显的表,我不想缓存它,因为当您输入表单时,缓存的版本会显示并且不会更新。有没有更好的方法来不缓存页面而不是元标记?

我现在使用的代码是

       <?php
       $query='select * from article order by `article`.`time` DESC LIMIT 10';
       $result=mysql_query($query); 
       echo '<table class="mytable" width="1000px" border="0">';
       while($row = mysql_fetch_array($result))
       {
    echo "<td><a href='".$row['url']."'>".$row['title']."</a> - ".$row['name']."</td><td>".$row['class']."</td></tr>";
}
echo '<table>';
       ?>

【问题讨论】:

    标签: php forms browser-cache


    【解决方案1】:
    <?php
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    ?>
    

    见http://php.net/manual/en/function.header.php

    【讨论】:

      【解决方案2】:

      它更复杂,但您可以将“非缓存”数据放在 iframe 中,并将 iframe 嵌入到您的主文档中。

      但是,除非您的主页非常大,否则我怀疑仅仅缓存页面的一部分是否值得。通常如果您的css、javascript 和images 都是外部的,它们应该被正常缓存并构成您的大部分代码。

      【讨论】:

      • 这是回答 OP 问题的唯一答案。
      【解决方案3】:

      用途:

      header("Cache-Control: post-check=0, pre-check=0", false); 
      header("Pragma: no-cache"); 
      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
      

      并使用ob 缓冲缓存其余部分。

      【讨论】:

        【解决方案4】:

        这些 php 标头将阻止客户端缓存您的 php 输出:

        <?php
        //no  cache headers 
        header("Expires: Mon, 26 Jul 2012 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        ?>
        

        【讨论】:

        • 这将阻止缓存整个页面。 OP 正在寻找缓存部分,而不是缓存其余部分。
        • 12012 应该是 2012
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多