找回密码
 新建账号

php header('Cache-Control: no-cache') 失效解决方法

[复制链接]
php 发表于 2016/9/27 16:36 | 显示全部楼层 |阅读模式
Cache-Control:no-store, no-cache, must-revalidate
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control:no-store, no-cache, no-transform, no-siteapp, must-revalidate
为了客户端缓存文件,在PHP中使用header('Cache-Control: ....')发送头信息,却发现无论怎么使用header(),每次得到的请求头都是
Cache-Control:no-store, no-cache, must-revalidate
Expires:Thu, 19 Nov 1981 08:52:00 GMT
请求头可以用get_headers($url, 1)来查看,其中$url是要请求的网页链接。

如果说Cache-Control变成no-store, no-cache, must-revalidate还可以理解,那么Expires:Thu, 19 Nov 1981 08:52:00 GMT这个日期又是从哪里来的呢??

实际上这两个头信息是php session模块里面发送的,是PHP程序源代码中写死的代码。调用session_start()以后,会自动发送这两个头信息。

要想改变php的这种行为,可以通过配置php.ini中的session.cache_limiter实现,也可以在php脚本中通过session_cache_limiter('none'),或者调用ini_set('session.cache_limiter', 'none')来改变该配置项的值,如果不是修改php.ini改变session_cache_limiter的值,则需要在调用session_start()之前通过脚本修改session.cache_limiter的值。

如果将session.cache_limiter设置为none, php不会发送任何禁止缓存的头信息,你需要自己在php脚本中使用header()发送头信息来禁止缓存。
session.cache_limiter的值可以为public private_no_expire private nocache,其中noncache是默认值,具体参数代表的意义可以查看PHP手册

  1. <?php
  2.         ini_set('session.cache_limiter', 'none');
  3.         session_start();
  4.         header('Pragma: no-cache');
  5.         header('Expires: ' . gmDate('D, d M Y H:i:s') . ' GMT');
  6.         header('Cache-Control: no-store, no-cache, must-revalidate, no-transform, no-siteapp');
  7. ?>
复制代码
以下代码是PHP源代码摘录,不是PHP脚本代码。
  1. CACHE_LIMITER_FUNC(nocache) /* {{{ */
  2. {
  3.     ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");

  4.     /* For HTTP/1.1 conforming clients and the rest (MSIE 5) */
  5.     ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");

  6.     /* For HTTP/1.0 conforming clients */
  7.     ADD_HEADER("Pragma: no-cache");
  8. }
复制代码

手机版|轻松E站

GMT+8, 2024/4/19 23:30

快速回复 返回顶部 返回列表