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

查看:92
本文介绍了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();
}
?>

推荐答案

最终解决了该错误.罪魁祸首是Gzip.由于我也将对If-Modified-Since和If-None-Match请求的响应都用gzip压缩,因此gzip在响应中添加了一些字节(有点像gzip标头).现在,我已停止gzip压缩对If-Modified-Since和If-None-Match请求的响应,它的工作原理就像是一种魅力.

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天全站免登陆