阻止浏览器缓存PHP请求

时间:2016-08-19 20:25:18

标签: apache .htaccess caching cache-control

我的网站上存在PHP文件缓存问题。

这是我的.htaccess文件现在的样子:

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "GET, POST"

# PHP Errors
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag log_errors on

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/opentype

# For Olders Browsers Which Can't Handle Compression
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

<IfModule mod_expires.c>

    # Enable expirations
    ExpiresActive On

    # Default directive
    ExpiresDefault "access plus 1 month"

    ExpiresByType image/x-icon "access plus 3 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"

    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType text/javascript "access plus 1 week"

</IfModule>

<filesMatch "\.php">
    FileETag None
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</filesMatch>

我有一个php脚本(A)[看起来像这个http://www.mywebsite.com/index.php?id=13&tc=xyhskfuw&ml=786w],它将数据[通过curl]发布到另一个脚本(B),然后(不管结果如何)重定向到html页面(C)。脚本(B)执行数据库操作。

我遇到的问题是脚本(A)似乎只运行一次,即使在浏览器的地址栏中重复发布后也是如此。它每次都重定向正确,但脚本(B)(应该从脚本(A)接收数据)不会运行多次。

我正在Firefox浏览器中测试它(打开开发人员工具),然后在网络选项卡打开的情况下运行脚本。在HTTP请求列表中,我看到.php脚本的状态代码为302,在 Transfer 标题下,它表示缓存

通过查看标题进一步检查,这是我看到的:

Access-Control-Allow-Headers: origin, x-requested-with, content-type
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Origin: *
Cache-Control: max-age=2592000
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Fri, 19 Aug 2016 19:21:39 GMT
Expires: Sun, 18 Sep 2016 19:21:39 GMT

我想防止这种情况发生。我该怎么做才能禁止浏览器缓存PHP请求?

1 个答案:

答案 0 :(得分:2)

我仔细研究了.htaccess文件后,设法解决了这个问题。

我注意到这个条目:ExpiresDefault "access plus 1 month"。事实证明,默认的缓存生命周期恰好是1个月,这可以解释max-age=2592000;是那个2592000秒弥补30天。我不知道它适用于php。

无论如何,我更新了上述条目:ExpiresDefault "access plus 0 seconds"

现在似乎工作正常。