开发者

What does header("Expires: 0") do? [duplicate]

This question already has answers here: HTTP Expires header values "0" and "-1" 开发者_开发技巧 (3 answers) Closed 7 years ago.

Can anyone explain this?


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

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.


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.


"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).

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")."


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.


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)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜