304未修改问题 [英] 304 Not Modified issue

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

问题描述

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

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();
}
?>

推荐答案

终于解决了这个bug.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.

这篇关于304未修改问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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