1

この 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;
}
4

3 に答える 3

3

このようなことができます。
ファイルの代わりに動的コンテンツの一意のハッシュを取得し、それを として設定します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;
?>
于 2013-08-13T21:05:17.653 に答える
1

get_include_files()を使用して、ファイルの生成に使用されたすべての PHP ファイルを取得できます。

初めてファイルを生成したら、そのリストをある種のストレージ (もちろん URL に関連付けられています) に保存し、そのリストから最大の filemtime() をクライアントに渡します。次回は、ファイル リストを取得し、ファイルの filemtime() が HTTP 要求で指定されたものよりも大きいかどうかを確認します。見つかったらすぐに再生成するか、見つからない場合はキャッシュされた応答を配信します。

明らかに、このアプローチは、応答の生成および/またはそれの送信が非常にパフォーマンスに負担がかかることを前提としています。そうしないと、これらすべての filemtime() チェックを実行すると、毎回ファイルを単純に再生成するよりも負担が大きくなる可能性があります。

于 2013-08-13T20:46:42.460 に答える
0

これは、暗号ハッシュよりも高速な 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;
}
?>
于 2016-07-24T16:03:41.867 に答える