PHP ETAG代使用PHP [英] php eTag generation using php

查看:127
本文介绍了PHP ETAG代使用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个PHP code生成一个XML文件的ETAG。问题是,只有当文件它的自我更新/修改ETAG更新。我需要的时候ETAG动态结果更新以及更新。任何想法,这可怎么办呢?

  //这样这个文件的最后修改日期
$ =上次更改filemtime(__ FILE__);//获得该文件的一个唯一的哈希(ETAG)
$ etagFile = md5_file(__ FILE__);//获取HTTP_IF_MODIFIED_SINCE头,如果集
$ ifModifiedSince =(使用isset($ _ SERVER ['HTTP_IF_MODIFIED_SINCE'])$ _ SERVER ['HTTP_IF_MODIFIED_SINCE']:假);//获取HTTP_IF_NONE_MATCH头如果设置(ETAG:唯一文件哈希)
$ etagHeader =(使用isset($ _ SERVER ['HTTP_IF_NONE_MATCH'])修剪($ _ SERVER ['HTTP_IF_NONE_MATCH']):假);//设置Last-Modified头
标题(上次修改:.gmdate(D,D M Y H:我:秒,$上次更改时间)GMT);//设置ETAG头
标题(Etag的:$ etagFile);//确保缓存已开启
//检查,如果页面发生了变化。如果没有,发送304和退出
如果(@strtotime($ _ SERVER ['HTTP_IF_MODIFIED_SINCE'])== $即早|| $ etagHeader == $ etagFile)
{
       标题(HTTP / 1.1 304未修改);
       出口;
}


解决方案

您可以做这样的事情。结果
获取动态内容的文件,而不是一个唯一的哈希并将其设置为的ETag

 < PHP
  $ LAST_MODIFIED = filemtime(__FILE__);  $ modified_since =(使用isset($ _ SERVER [HTTP_IF_MODIFIED_SINCE])的strtotime($ _ SERVER [HTTP_IF_MODIFIED_SINCE]):假);
  $ etagHeader =(使用isset($ _ SERVER [HTTP_IF_NONE_MATCH])修剪($ _ SERVER [HTTP_IF_NONE_MATCH]):假);  //这是从这个文件的实际输出(在你的情况下,XML数据)
  $内容='从数据库或文件的XML数据';
  //生成的输出ETAG
  $ ETAG = sprintf的(%S-%的,$ LAST_MODIFIED,MD5($内容));  //设置Last-Modified头
  标题(上次修改:.gmdate(D,D M Y H:I:S$ LAST_MODIFIED)GMT);
  //设置ETAG头
  标题(Etag的:$ ETAG);  //如果最后修改日期是一样的HTTP_IF_MODIFIED_SINCE,发送304然后退出
  如果((INT)$ modified_since ===(INT)$ LAST_MODIFIED和放大器;&安培; $ ETAG === $ etagHeader){
    标题(HTTP / 1.1 304未修改);
    出口;
  }  //新的内容或文件的修改,所以输出ypur内容
  回声$内容;
  出口;
?>

This PHP code generates an eTag for an xml file. The problem is eTag updates only when the file it self is updated/modified. I need the etag to update when the dynamic results are updated as well. any idea how this can be done?

//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;
}

解决方案

You can do something like this.
Get a unique hash of dynamic content instead of file and set it as 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;
?>

这篇关于PHP ETAG代使用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆