【问题标题】:How to break ob_start() and continue after some tags如何打破 ob_start() 并在一些标签后继续
【发布时间】:2012-10-11 08:14:44
【问题描述】:

我想为一个电子商务平台构建一个缓存系统。

我选择在页面末尾使用ob_start('callback')ob_end_flush()

我将验证是否为访问的 url 创建了任何 .cache 文件,如果有文件我将打印其内容。

我的问题是我想让购物车保持活动状态,所以我不想缓存它。我怎样才能做到这一点?

<?php

    function my_cache_function($content) {
        return $content;
    }

    ob_start('my_cache_function');

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
</head>
<body>
     test
     <?php
         //some ob_break() ?
     ?>
     <div id="shopping-cart">
         this should be the content I do not want to cache it
     </div>
     <?php
         // ob_continue() ?
     ?>

</body>
</html>
<?php
     ob_end_flush();
?>

提前谢谢你!

【问题讨论】:

    标签: php caching ob-start


    【解决方案1】:

    如果你这样做,问题是内容将在任何之前放置的 HTML 之前输出。您可能想要将该内容保存在某个变量中,然后在缓存“模板”文件中使用占位符,例如 %SHOPPING-CART%

    因此,您可以用 str_replace 将其替换为真正的非缓存内容。

    【讨论】:

    • 即使用户未登录,购物车也将可用。当用户登录时,不会有缓存,所以我不想缓存的唯一项目是购物车,它将在每一页上都可见。我相信您的解决方案会奏效,所以感谢您的回答。我不明白为什么我直到现在才想到这个:))
    【解决方案2】:

    你可以这样做:

    <?php
    
        function my_cache_function($content) {
            return $content;
        }
        $output = "";
        ob_start('my_cache_function');
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>test</title>
    </head>
    <body>
         test
         <?php
             $output .= ob_get_clean();
         ?>
         <div id="shopping-cart">
             this should be the content I do not want to cache it
         </div>
         <?php
             ob_start();
         ?>
    
    </body>
    </html>
    <?php
             $output .= ob_get_clean();
             echo $output;
    ?>
    

    尽管这并没有真正的意义。

    【讨论】:

      【解决方案3】:

      我不确定 Zulakis 的解决方案是否能顺利完成……这个改动怎么样?

      <?php
      $pleaseCache=true;
      function my_cache_function($content) {
          if($pleaseCache)
          {
              /// do your caching
          }
          return $content;
      }
      $output = "";
      ob_start('my_cache_function');
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title>test</title>
      </head>
      <body>
           test
           <?php
               $output .= ob_get_clean();
               $pleaseCache = false;
               ob_start('my_cache_function');
           ?>
           <div id="shopping-cart">
               this should be the content I do not want to cache it
           </div>
           <?php
               $output .= ob_get_clean();
               $pleaseCache = true;
               ob_start('my_cache_function');
           ?>
      
      </body>
      </html>
      <?php
           $output .= ob_get_clean();
           ob_end_clean();
           echo $output;
      ?>
      

      再说一次,我不确定这是否有意义......但我假设你有你的理由。

      【讨论】:

      • 谢谢你的回答。我给了你和@Zulakis +1,因为你已经尝试了我所要求的。不幸的是,有一个误解,我认为 blue112 解决方案是最好的。即使我使用您的解决方案,如果我从缓存文件中获取购物车,购物车也不会生效
      猜你喜欢
      • 2018-04-20
      • 2015-11-24
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多