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

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

Understand If-Modified-Since HTTP Header

phphttpcache-controloutputcacheif-modified-since

提问by Mike

I am looking at a Caching library that is trying to use the If-Modified-Sinceheader 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-Sinceheader? 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-Sinceonly makes sense if the client already has a resource which is obtained along with a response that has a Last-Modifiedheader in combination with headers which allow browser caching like a Cache-Controland/or Pragmavalue containing public.

If-Modified-Since仅当客户端已经拥有一个资源以及响应时,请求才有意义,该响应具有Last-Modified标头与标头组合,允许浏览器缓存像 aCache-Control和/或Pragma包含public.

Also, I've noticed that some browsers does not include If-Modified-Sincewhen the original response also contained an ETagheader. The browser will instead use If-None-Matchto 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-Modifiedis needed as a validator (do not send ETagif you want to test for If-Modified-Since)
  • Expires -1tells that the resource is stale and must always be revalidated
  • Cache-Controlmust 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/200response, on subsequent requests, the browser should send conditional requests that include the If-Modified-Sinceheader.

当您在初始HTTP/200响应中发送这些标头时,在后续请求中,浏览器应发送包含If-Modified-Since标头的条件请求。