【问题标题】:php eTag generation using php使用 php 生成电子标签
【发布时间】:2013-08-13 20:30:29
【问题描述】:

此 PHP 代码为 xml 文件生成一个 eTag。问题是 eTag 仅在其自身文件被更新/修改时才更新。当动态结果更新时,我也需要更新 etag。知道如何做到这一点吗?

//get the last-modified-date of this very file
$lastModified=filemtime(__FILE__);

//get a unique hash of this file (etag)
$etagFile = md5_file(__FILE__);

//get the HTTP_IF_MODIFIED_SINCE header if set
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);

//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);

//set last-modified header
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");

//set etag-header
header("Etag: $etagFile");

//make sure caching is turned on    
//check if page has changed. If not, send 304 and exit
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile)
{
       header("HTTP/1.1 304 Not Modified");
       exit;
}

【问题讨论】:

  • 获取动态内容的唯一哈希而不是文件,并将其设置为eTag。喜欢$etagFile = md5( $your_output );
  • 将其设置为一个唯一值,表示您的最后一次更新。可能是您数据中的一个简单的最后更新时间戳,但如果 1 秒内可能有多次更改,这有点容易受到攻击,在这种情况下,您需要添加最后一个内容的标识符。类似于伪代码md5(md5file.max(mtimefile,mtimedata).identifierlastupdatedrecord);
  • 你能详细说明一下吗?我如何获得动态内容的唯一哈希? @bystwn22
  • @apak 看看我的回答,希望对你有帮助。

标签: php xml apache cache-control etag


【解决方案1】:

你可以这样做。
获取动态内容的唯一哈希而不是文件并将其设置为eTag

<?php
  $last_modified  = filemtime( __FILE__ );

  $modified_since = ( isset( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ? strtotime( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) : false );
  $etagHeader     = ( isset( $_SERVER["HTTP_IF_NONE_MATCH"] ) ? trim( $_SERVER["HTTP_IF_NONE_MATCH"] ) : false );

  // This is the actual output from this file (in your case the xml data)
  $content  = 'your xml data from db or file';
  // generate the etag from your output
  $etag     = sprintf( '"%s-%s"', $last_modified, md5( $content ) );

  //set last-modified header
  header( "Last-Modified: ".gmdate( "D, d M Y H:i:s", $last_modified )." GMT" );
  //set etag-header
  header( "Etag: ".$etag );

  // if last modified date is same as "HTTP_IF_MODIFIED_SINCE", send 304 then exit
  if ( (int)$modified_since === (int)$last_modified && $etag === $etagHeader ) {
    header( "HTTP/1.1 304 Not Modified" );
    exit;
  }

  // new content or file modified, so output ypur content
  echo $content;
  exit;
?>

【讨论】:

  • 谢谢@bystwn22!修复了它:)
【解决方案2】:

您可以使用get_included_files()获取所有已用于生成文件的PHP文件。

第一次生成文件后,将该列表存储在某种存储中(当然,与 URL 相关联),并将该列表中最大的 filemtime() 提供给客户端。下一次,检索文件列表,并检查是否有任何文件的 filemtime() 大于 HTTP 请求中给定的值。找到后立即重新生成,如果找不到,则传递缓存的响应。

显然,这种方法假定生成响应和/或发送响应的性能成本很高。否则,执行所有这些 filemtime() 检查最终可能比每次都重新生成文件更费力。

【讨论】:

    【解决方案3】:

    这是我的实际生产代码,使用比加密哈希更快的 CRC32。

    <?php
    // pull xml feed from wordpress blogs! then build an etag from the crc32 of the content! BOOOOMMMMMMMM
    $xml=("http://www.funk.co.nz/auckland-music-update/feed/");
    $xmlDoc = new DOMDocument();
    $xmlDoc->load($xml);
    $last_modified  = filemtime( __FILE__ );
    $modified_since = ( isset( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ? strtotime( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) : false );
    $etagHeader     = ( isset( $_SERVER["HTTP_IF_NONE_MATCH"] ) ? trim( $_SERVER["HTTP_IF_NONE_MATCH"] ) : false );
    // This is the actual output from stream
    $content = $xmlDoc->saveXML() . "\n";
    // pull a set of blogs
    $items = $xmlDoc->getElementsByTagName('item');
    // pull out the last blog title (for use in page)
    $latestBlog=$items->item(0)->getElementsByTagName('title')->item(0)->nodeValue;
    // generate the etag from xml content
    $etag = sprintf( '"%s-%s"', $last_modified, crc32( $content ) );
    //set last-modified header
    header( "Last-Modified: ".gmdate( "D, d M Y H:i:s", $last_modified )." GMT" );
    //set etag-header
    header( "Etag: ".$etag );
    // if last modified date is same as "HTTP_IF_MODIFIED_SINCE", send 304 then exit
    if ( (int)$modified_since === (int)$last_modified && $etag === $etagHeader ) {
      header( "HTTP/1.1 304 Not Modified" );
      exit;
    }
    ?>
    

    【讨论】:

    • 好像是评论。
    猜你喜欢
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2018-08-14
    相关资源
    最近更新 更多