304 未修改问题

发布于 2024-08-25 10:25:18 字数 407 浏览 5 评论 0原文

抱歉,标题可能有误。我正在编写一些代码来处理 If-Modified-Since 和 If-None-Match 请求作为缓存的一部分。除了 PHP 在标头后面返回一些内容(空行)之外,一切都很完美。页面内容应该为空。我正在使用的代码是:

<?php
$lastmod = filemtime($f);
$etag = '"'.dechex($lastmod).'"';
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_mod || $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
  header('HTTP/1.1 304 Not Modified');
  header('Content-Length: 0');
  exit();
}
?>

Sorry for the probably wrong title. I am writing some code to handle If-Modified-Since and If-None-Match requests as part of caching. Everything works perfect except for that PHP returns some content (an empty line) after the headers. The page content should be empty instead. The code that I am using is:

<?php
$lastmod = filemtime($f);
$etag = '"'.dechex($lastmod).'"';
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_mod || $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
  header('HTTP/1.1 304 Not Modified');
  header('Content-Length: 0');
  exit();
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩起发的微风 2024-09-01 10:25:18

终于解决了这个bug。 Gzip 是罪魁祸首。由于我也在 gzip 压缩对 If-Modified-Since 和 If-None-Match 请求的响应,因此 gzip 向响应添加了一些字节(类似于 gzip 标头)。现在我已经停止对 If-Modified-Since 和 If-None-Match 请求的响应进行 gzip 压缩,它就像一个魅力。

Finally solved this bug. Gzip was the culprit. Since I was gzipping the responses to If-Modified-Since and If-None-Match requests too, gzip was adding a few bytes (kind of a gzip header) to the response. Now I have stopped gzipping responses to If-Modified-Since and If-None-Match requests, and it works like a charm.

泪意 2024-09-01 10:25:18

试试这个代码:

$last_modified = filemtime($f);
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
    $expected_modified = strtotime(preg_replace('/;.*$/','',$_SERVER["HTTP_IF_MODIFIED_SINCE"]));
    if($last_modified <= $expected_modified) {
        header("HTTP/1.0 304 Not Modified");
        exit;
    }
}

Try this code:

$last_modified = filemtime($f);
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
    $expected_modified = strtotime(preg_replace('/;.*$/','',$_SERVER["HTTP_IF_MODIFIED_SINCE"]));
    if($last_modified <= $expected_modified) {
        header("HTTP/1.0 304 Not Modified");
        exit;
    }
}
青衫负雪 2024-09-01 10:25:18

我在 回答 HTTP_IF_MODIFIED_SINCE 中找到了解决方案PHP 中的 HTTP_IF_NONE_MATCH

创建新文件 caching_headers.php

<?php
function caching_headers($file,$timestamp){
    $gmt_mtime=gmdate('r', $timestamp);
    header('ETag: "'.md5($timestamp.$file).'"');
    if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])||isset($_SERVER['HTTP_IF_NONE_MATCH'])){
        if ($_SERVER['HTTP_IF_MODIFIED_SINCE']==$gmt_mtime||str_replace('"','',stripslashes($_SERVER['HTTP_IF_NONE_MATCH']))==md5($timestamp.$file)){
            header('HTTP/1.1 304 Not Modified');
            exit();
        }
    }
    header('Last-Modified: '.$gmt_mtime);
    header('Cache-Control: public');
}
?>

并将其添加到您要缓存的所有 php 文件中:

<?php
include('caching_headers.php');
caching_headers($_SERVER['SCRIPT_FILENAME'],filemtime($_SERVER['SCRIPT_FILENAME']));
?>

I found the solution in Answering HTTP_IF_MODIFIED_SINCE and HTTP_IF_NONE_MATCH in PHP

Create new file caching_headers.php

<?php
function caching_headers($file,$timestamp){
    $gmt_mtime=gmdate('r', $timestamp);
    header('ETag: "'.md5($timestamp.$file).'"');
    if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])||isset($_SERVER['HTTP_IF_NONE_MATCH'])){
        if ($_SERVER['HTTP_IF_MODIFIED_SINCE']==$gmt_mtime||str_replace('"','',stripslashes($_SERVER['HTTP_IF_NONE_MATCH']))==md5($timestamp.$file)){
            header('HTTP/1.1 304 Not Modified');
            exit();
        }
    }
    header('Last-Modified: '.$gmt_mtime);
    header('Cache-Control: public');
}
?>

and add this in all php files that you would like to cache:

<?php
include('caching_headers.php');
caching_headers($_SERVER['SCRIPT_FILENAME'],filemtime($_SERVER['SCRIPT_FILENAME']));
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文