ObiHill
ObiHill

Reputation: 11886

Prevent the browser from caching PHP requests

I'm having an issue with PHP file caching on my website.

Here's what my .htaccess file looks like right now:

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>

I have a php script (A) [that looks something like this http://www.mywebsite.com/index.php?id=13&tc=xyhskfuw&ml=786w] which posts data [via curl] to another script (B), and then (irrespective of the result) redirects to a html page (C). The script (B) does a database operation.

The problem I'm having is that script (A) only seems to run once, even after repeated posts in the browser's address bar. It redirects correctly every time, but script (B) (which is supposed to receive data from script (A)) doesn't run more than once.

I'm testing this in the Firefox browser (with developer tools open), and I ran the script with the Network tab open. In the list of HTTP requests, I see that the .php script has a Status Code of 302, and under the Transfer header it says cached.

On further inspection by looking at the headers, here's what I see:

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

I'd like to prevent this from happening. What can I do to disable the browser from caching PHP requests?

Upvotes: 0

Views: 600

Answers (1)

ObiHill
ObiHill

Reputation: 11886

I managed to figure this out after carefully poring over the .htaccess file.

I noticed this entry: ExpiresDefault "access plus 1 month". It turns out the default caching lifecycle is exactly 1 month, which would explain max-age=2592000; being that 2592000 seconds make up 30 days. I had no idea it applied to php though.

Anyway, I updated said entry to this: ExpiresDefault "access plus 0 seconds".

It seems to be working fine now.

Upvotes: 2

Related Questions