浏览器不侦听 PHP 发送的标头进行缓存,可以更改什么?
我有一个系统,在网站上有两层缓存。每个请求都会检查 Web 服务器上的缓存,如果发现已缓存的生成页面,则返回缓存而不是再次生成。
例如:
- 用户 A 访问网站 URL
http://www.example.com/home/
- 服务器未找到缓存
- 服务器生成页面
- 服务器将页面写入缓存
- 服务器将生成的页面返回给用户 A
- 用户 A 很高兴
并且
- 用户 B 访问网站 URL
http://www.example.com/home/
- 服务器找到缓存
- 服务器返回缓存 而不是再次生成页面
- 用户 B 很高兴
一切顺利, 。但我还想添加一个选项,即浏览器不会再次 ping 服务器(节省服务器检查缓存是否存在的时间)并使用自己的缓存。
- 用户 A 再次访问 URL
http://www.example.com/home/
- 浏览器在缓存中具有该页面 浏览
- 器从缓存中为用户加载页面
我无法让后者工作。在原始页面生成期间,我向页面的用户发送以下标头:
header('Cache-Control: public, max-age=10000, must-revalidate');
header('Expires: Fri, 03 Feb 2012 01:59:45 GMT');
但是当我使用 Firebug 或 Chrome 开发人员工具检查它时,它并没有说它正在使用缓存,而是再次从服务器请求数据。我知道我一定做错了什么,因为我为 Javascript 之类的静态文件设置了相同的设置,并且有效。
为了测试这一点,我不仅尝试重新加载页面,还在网站上创建了链接,并在每次从服务器请求页面的链接之间移动。
我错过了什么吗?
编辑:
好吧,显然发生的事情是服务器每次都会自动发送“Pragma:no-cache”。有谁知道为什么服务器会这样做?这使得浏览器无法使用缓存。
I have a systen in place where I have two-layered cache on the website. Every request checks for cache on the web server and if it finds the generated page that has been cached, it returns the cache instead of generating it again.
For example:
- User A visits website URL
http://www.example.com/home/
- Server does not find cache
- Server generates the page
- Server writes the page to cache
- Server returns the generated page to User A
- User A is happy
and
- User B visits website URL
http://www.example.com/home/
- Server finds cache
- Server returns the cache instead of generating the page again
- User B is happy
All that works without problems. But I also want to add an option that browser would not ping the server again (saving servers time of checking if cache exists or not) and use its own cache instead.
- User A visits URL
http://www.example.com/home/
again - Browser has that page in cache
- Browser loads the page for the user from cache
I cannot get the latter working. During original page generation, I am sending to the user with the page the following headers:
header('Cache-Control: public, max-age=10000, must-revalidate');
header('Expires: Fri, 03 Feb 2012 01:59:45 GMT');
But when I check for it with Firebug or Chrome Developer Tools it does not say it is using a cache, instead asking for the data from the server again. I know I must be doing something wrong, since I have the same thing set up for static files like Javascript, and that works.
To test this I didn't just try reloading the page, I created links on the website and moving between those links it asked for the pages from server each time.
Am I missing something?
EDIT:
Alright, apparently what happened was that server sent "Pragma: no-cache" automatically every time. Does anyone know why server would do that? This kept the browser from using cache.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果该页面/url 启用了会话,则
Pragma: no-cache
标头将添加到 http 标头中,以防止浏览器使用缓存。If session is enabled for that page/url, the
Pragma: no-cache
-header will be added to the http header which prevents the browser from using the cache.如果你使用session_start
PHP 将添加
PHP 的全部意义在于提供动态页面。
阻止这个......
然后你可以根据你提供的内容编写你自己的标题
if you use session_start
PHP will add
the whole point of PHP is to provide dynamic pages after all.
to stop this ...
and you can then write your own headers based on the content you provide