如何在PHP文件中使用ETag?
如何在PHP文件中实现ETag?我应该上传什么到服务器上,在PHP文件中插入什么?创建/编辑.htaccess文件并添加以下内容:如何在PHP文件中使用ETag?,php,caching,header,etag,Php,Caching,Header,Etag,如何在PHP文件中实现ETag?我应该上传什么到服务器上,在PHP文件中插入什么?创建/编辑.htaccess文件并添加以下内容: FileETag MTime Size 将以下内容放在函数中或放在需要ETag处理的PHP文件的顶部: <?php $file = 'myfile.php'; $last_modified_time = filemtime($file); $etag = md5_file($file); header("Last-Mo
FileETag MTime Size
将以下内容放在函数中或放在需要ETag处理的PHP文件的顶部:
<?php
$file = 'myfile.php';
$last_modified_time = filemtime($file);
$etag = md5_file($file);
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: $etag");
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}
?>
必须引用与etag值对应的版本:
<?php
$file = __DIR__ . '/myfile.js';
$etag = '"' . filemtime($file) . '"';
// Use it if the file is changed more often than one time per second:
// $etag = '"' . md5_file($file) . '"';
header('Etag: ' . $etag);
$ifNoneMatch = array_map('trim', explode(',', trim($_SERVER['HTTP_IF_NONE_MATCH'])));
if (in_array($etag, $ifNoneMatch, true) || count($ifNoneMatch) == 1 && in_array('*', $ifNoneMatch, true)) {
header('HTTP/1.1 304 Not Modified');
exit;
}
print file_get_contents($file);
我遇到过这样的情况,我不得不在$\u SERVER['HTTP\u IF\u NONE\u MATCH']@lordspace中修剪周围的单引号/双引号。什么情况?这是不是导致脚本无法工作?我记不起我正在使用的确切产品,但etag字符串是用双引号传递的,所以我不得不使用trim$etag,'\';请注意,这基本上只在PHP文件不包含任何其他文件的情况下起作用。在更新其他文件时,不会更改ETag。ETag值必须包含引号,