php 了解 If-Modified-Since HTTP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5081397/
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
Understand If-Modified-Since HTTP Header
提问by Mike
I am looking at a Caching library that is trying to use the If-Modified-Since
header of a request object. The problem is this header never gets set, it is always blank which makes sense to me seeing how it is a REQUEST.
我正在查看一个尝试使用If-Modified-Since
请求对象标头的缓存库。问题是这个标题永远不会被设置,它总是空白,这对我来说是有意义的,因为它是一个请求。
How can you force a request to have a If-Modified-Since
header? Or am I way off for what this does.
如何强制请求具有If-Modified-Since
标头?或者我对它所做的事情有什么看法。
Here is the function I am referring to.
这是我所指的功能。
public function isNotModified(Request $request)
{
$lastModified = $request->headers->get('If-Modified-Since');
$notModified = false;
if ($etags = $request->getEtags()) {
$notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
} elseif ($lastModified) {
$notModified = $lastModified == $this->headers->get('Last-Modified');
}
if ($notModified) {
$this->setNotModified();
}
return $notModified;
}
回答by BalusC
A request with If-Modified-Since
only makes sense if the client already has a resource which is obtained along with a response that has a Last-Modified
header in combination with headers which allow browser caching like a Cache-Control
and/or Pragma
value containing public
.
If-Modified-Since
仅当客户端已经拥有一个资源以及响应时,请求才有意义,该响应具有Last-Modified
标头与标头组合,允许浏览器缓存像 aCache-Control
和/或Pragma
包含public
.
Also, I've noticed that some browsers does not include If-Modified-Since
when the original response also contained an ETag
header. The browser will instead use If-None-Match
to test it.
另外,我注意到某些浏览器不包括If-Modified-Since
原始响应何时还包含ETag
标头。浏览器将使用If-None-Match
它来测试它。
See also:
也可以看看:
回答by alienhard
First you have to make sure the initial response is cached in the first place (I answeredthis in another, related question.
首先,您必须确保首先缓存初始响应(我在另一个相关问题中回答了这个问题。
Try to set the following fields:
尝试设置以下字段:
Last-Modified: Wed, 16 Feb 2011 13:52:26 GMT
Expires: -1
Cache-Control: must-revalidate, private
Last-Modified
is needed as a validator (do not sendETag
if you want to test forIf-Modified-Since
)Expires -1
tells that the resource is stale and must always be revalidatedCache-Control
must not include no-cache nor no-store
Last-Modified
需要作为验证器(ETag
如果要测试,请不要发送If-Modified-Since
)Expires -1
告诉资源是陈旧的,必须始终重新验证Cache-Control
不得包含 no-cache 或 no-store
When you send these headers on the initial HTTP/200
response, on subsequent requests, the browser should send conditional requests that include the If-Modified-Since
header.
当您在初始HTTP/200
响应中发送这些标头时,在后续请求中,浏览器应发送包含If-Modified-Since
标头的条件请求。