首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php eTag生成

php eTag生成
EN

Stack Overflow用户
提问于 2013-08-14 04:30:30
回答 3查看 7.2K关注 0票数 1

这段代码为xml文件生成一个eTag。问题是eTag只有在其自身的文件被更新/修改时才会更新。我需要在动态结果更新时更新etag。你知道如何做到这一点吗?

代码语言:javascript
复制
//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;
}
EN

回答 3

Stack Overflow用户

发布于 2013-08-14 05:05:18

你可以做这样的事情。

获取动态内容而不是文件的唯一散列,并将其设置为eTag

代码语言:javascript
复制
<?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;
?>
票数 3
EN

Stack Overflow用户

发布于 2013-08-14 04:46:42

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

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

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

票数 1
EN

Stack Overflow用户

发布于 2016-07-25 00:03:42

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

代码语言:javascript
复制
<?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;
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18218643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档