title: Browser Caching dynamic content using PHP output buffer and ETag
body: <p>I have been researching some strategies to optimize a web application I am working on particularly related to web browser caching and dynamic data. Since potentially the same dynamic content may be loaded multiple times in a session, I came up with the following method using PHP's output buffer and using a hash of the content as an ETag.</p>
  
  <p>I realize that the only thing I really save with this method is the transfer of data back to the user since the PHP script still has to completely run, but I was curious if anyone has done something similar and if there are any thoughts or concerns I should be aware of or what other methods may be better.</p>
  
  <p>Here is the code I am including at the top of each page:</p>
  
  <pre><code>&lt;?php
  function hash_buffer($content) {
      $buffer_hash = crc32($content);
      if ($_SERVER['HTTP_IF_NONE_MATCH'] == $buffer_hash) {
          header('HTTP/1.1 304 Not Modified');
          header("ETag: $buffer_hash");
          return '';
      }
      header('Cache-Control: private, no-cache');
      header("ETag: $buffer_hash");
      return $content;
  }
  
  ob_start('hash_buffer');
  ?&gt;
  </code></pre>