如何在 PHP 文件中使用 etags?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13197479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:56:59  来源:igfitidea点击:

How to use etags in a PHP file?

phpcachingheaderetag

提问by Nagra

How do you implemented etags inside a PHP file? What do I upload to the server and what do I insert into my PHP file?

你如何在 PHP 文件中实现 etags?我将什么上传到服务器以及我在 PHP 文件中插入什么?

回答by Nagra

Create / edit your .htaccess file and add the following:

创建/编辑您的 .htaccess 文件并添加以下内容:

FileETag MTime Size

Either place the following inside a function or put it at the top of the PHP file that you need etags to work on:

将以下内容放在函数中或将其放在需要 etags 处理的 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; 
} 
?>