使用php生成php eTag

使用php生成php eTag,php,xml,apache,cache-control,etag,Php,Xml,Apache,Cache Control,Etag,此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_MODIF

此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;
}
您可以使用获取用于生成文件的所有PHP文件

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

显然,这种方法假设生成和/或发送响应的性能代价非常高。否则,执行所有这些filemtime()检查可能会比每次简单地重新生成文件更费力。

您可以使用来获取用于生成文件的所有PHP文件

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


显然,这种方法假设生成和/或发送响应的性能代价非常高。否则,执行所有这些filemtime()检查可能会比每次简单地重新生成文件更费力。

您可以这样做。
获取动态内容而不是文件的唯一哈希,并将其设置为
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;
?>

您可以这样做。
获取动态内容而不是文件的唯一哈希,并将其设置为
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;
?>

这是我实际的生产代码,使用比加密哈希更快的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;
}
?>

这是我实际的生产代码,使用比加密哈希更快的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;
}
?>


获取动态内容的唯一哈希,而不是文件,并将其设置为
eTag
。比如
$etagFile=md5($your_输出)将其设置为表示上次更新的唯一值。可能是数据中的一个简单的最后更新时间戳,但如果在1秒内有多个更改,则有点容易受到攻击,在这种情况下,您需要添加最后内容的标识符。类似伪代码
md5(md5file.max(mtimefile,mtimedata).identifierLastUpdateRecord)你能详细解释一下吗?如何获取动态内容的唯一散列@bystwn22@apak查看我的答案,希望它能帮助您。获取动态内容的唯一哈希值,而不是文件,并将其设置为
eTag
。比如
$etagFile=md5($your_输出)将其设置为表示上次更新的唯一值。可能是数据中的一个简单的最后更新时间戳,但如果在1秒内有多个更改,则有点容易受到攻击,在这种情况下,您需要添加最后内容的标识符。类似伪代码
md5(md5file.max(mtimefile,mtimedata).identifierLastUpdateRecord)你能详细解释一下吗?如何获取动态内容的唯一散列@bystwn22@apak看看我的答案,希望对你有帮助。谢谢@bystwn22!这就解决了:)谢谢@bystwn22!这修正了它:)它看起来像评论。它看起来像评论。