php header("Expires: 0") 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4516190/
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 13:16:08  来源:igfitidea点击:

What does header("Expires: 0") do?

phphttp

提问by Mohsin Sheikh Khalid

Can anyone explain this?

谁能解释一下?

采纳答案by RabidFire

Generally used to prevent caching. However, this document will probably provide you with a better explanation:

一般用于防止缓存。但是,本文档可能会为您提供更好的解释:

http://download.oracle.com/docs/cd/E13158_01/alui/wci/docs103/devguide/tsk_pagelets_settingcaching_httpexpires.html

http://download.oracle.com/docs/cd/E13158_01/alui/wci/docs103/devguide/tsk_pagelets_settingcaching_httpexpires.html

To quote:

报价:

Never use Expires = 0 to prevent caching. The Expires header is sent by the remote server and passed through to the browser by the Portal Server. Unless the time on all three machines is synchronized, an Expires=0 header can mistakenly return cached content. To solve this problem, set the Expires header to a fixed date that is definitely in the past.

永远不要使用 Expires = 0 来防止缓存。Expires 标头由远程服务器发送并由门户服务器传递到浏览器。除非所有三台机器上的时间都同步,否则 Expires=0 标头可能会错误地返回缓存的内容。要解决此问题,请将 Expires 标头设置为绝对过去的固定日期。

回答by Tyler Carter

How caching works (among other things) is that you send a header to the browser telling it when the page's content will expire. This means that if you send a header like:

缓存的工作原理(除其他外)是您向浏览器发送一个标头,告诉它页面内容何时到期。这意味着如果你发送一个像这样的标头:

header("Expires Sunday June 10th 2011"); // not correct timestamp

The content won't 'expire' until that date. The browser can then use caching techniques to serve the page locally from your browser instead of having to download all the content again.

内容在该日期之前不会“过期”。然后浏览器可以使用缓存技术从您的浏览器本地提供页面,而不必再次下载所有内容。

When you set the expiration to 0, it ensures that the next time the browser loads the page, it will download the content, thus giving you up to the second data. However, as you might see from other answers, you shouldn't send 0 to prevent caching, but instead send a date that is in the past.

当您将到期时间设置为 0 时,它可以确保浏览器下次加载页面时,将下载内容,从而为您提供第二个数据。但是,正如您从其他答案中看到的那样,您不应该发送 0 以防止缓存,而是发送过去的日期。

回答by Jon Moore

"Expires: 0" means that a cache will always treat this entry as stale (i.e. it will need to revalidate it first before returning it to a client).

"Expires: 0" 意味着缓存将始终将此条目视为陈旧的(即,在将其返回给客户端之前,它需要先重新验证它)。

From the definition of the Expires header in the HTTP/1.1 RFC (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21): "HTTP/1.1 clients and caches MUST treat other invalid date formats, especially including the value "0", as in the past (i.e., "already expired")."

根据 HTTP/1.1 RFC ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21) 中Expires 标头的定义:“HTTP/1.1 客户端和缓存必须处理其他无效的日期格式,特别是包括值“0”,就像过去一样(即“已经过期”)。”

回答by pooja

some browsers always reload stylesheets, javascripts and other seldomnly changing files, which causes nasty delays when loading a website (Safari on MacOS is an example)

一些浏览器总是重新加载样式表、javascripts 和其他很少更改的文件,这会导致加载网站时出现令人讨厌的延迟(MacOS 上的 Safari 就是一个例子)

to tell the browser to keep files in cache for at least a day, you can use This has the nice sideeffect of telling other browser that never refresh pages to refresh them at least once a day.

告诉浏览器将文件保留在缓存中至少一天,您可以使用这有一个很好的副作用,告诉其他浏览器从不刷新页面至少每天刷新一次。

回答by EnabrenTane

Sets the cache expiration header. This tells the browser how long they are allowed to keep it cached before they must re fetch it.

设置缓存过期标头。这告诉浏览器在必须重新获取它之前允许将其缓存多长时间。

I believe 0 means it never expires which can cause all kinds of problems. I believe the unit is seconds. On the other hand it might mean it cannot be cached.

我相信 0 意味着它永远不会过期,这可能会导致各种问题。我相信单位是秒。另一方面,这可能意味着它不能被缓存。