caching headers from PHP

梦想的初衷 提交于 2019-12-10 20:00:02

问题


In PHP, by default no cache related headers are sent.

HTTP/1.1 200 OK
Date: Fri, 19 Nov 2010 11:02:16 GMT
Server: Apache/2.2.15 (Win32) PHP/5.2.9-2
X-Powered-By: PHP/5.2.9-2
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 26
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

Now, since by default it does not say anything about caching, can it result in say example.com/index.php getting cached in some situations?


回答1:


Yes. In general, every successful response may be cached unless there are some constrains:

Unless specifically constrained by a cache-control (section 14.9) directive, a caching system MAY always store a successful response (see section 13.8) as a cache entry, MAY return it without validation if it is fresh, and MAY return it after successful validation.




回答2:


Yes, usually the browser will cache certain files by default (usually images and css) if no rules have been setup on the server-side (see browser cache).

You can set up cache-control headers to control this, or disable it completely using:

header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");

See example #2 in header and read the note below it.




回答3:


can it result in say example.com/index.php getting cached in some situations?

It shouldn't, however there's a lot of implementations out there (particularly on mobile devices / mobile proxies) which don't behave correctly in this regard.

There's also also a lot of bad information about caching - the 'Pragma: no-cache' is meaningless when sent from a server.

To prevent caching:

header("Cache-Control: no-store, no-cache, must-revalidate"); 

When all else fails - check the manual



来源:https://stackoverflow.com/questions/4224485/caching-headers-from-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!