2

I have an application where a number of otherwise static javascript files are being generated via PHP to allow configuration options to alter the static files (path like: mystaticfile.js.php). Everything works fine EXCEPT that I can't seem to get the cache settings to work and these resources are being reloaded with every page load.

The PHP file uses the following headers to try to set the cache settings:

$expires= 60 * 60 * 24 * 60; //cache for 60 days
header('Pragma: public');
header('Cache-Control: max-age=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
header("content-type: application/x-javascript");

However, when the files are served they're showing headers that look like:

HTTP/1.1 200 OK
Date: Sun, 06 Nov 2016 19:18:00 GMT
Server: Apache/2.2.15 (CentOS)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 744
Keep-Alive: timeout=15, max=95
Connection: Keep-Alive
Content-Type: application/x-javascript

My first thought was that it was that it might be because Apache has the ExpiresActive flag set on but I don't see any ExpiresByType rules set for PHP files.

Reading online it sounds like ETag issues could be the problem, but I've added

Header unset Pragma
FileETag None
Header unset ETag 

to the http.conf file (and restarted the service) and still no dice.

Any thoughts?

8
  • how about making them *.js the browser would likely cache that Commented Nov 6, 2016 at 20:10
  • @nogad it shouldn't make any difference. The headers are being set and the browser recognizes them as JS files. The issue is that the browser is receiving instructions not to cache despite the headers being set by php Commented Nov 6, 2016 at 20:44
  • Can you try to add some random header (header('Foo: bar');), just to first make sure your headers are applied? Commented Nov 6, 2016 at 21:51
  • i agree with shouldn't, but that's no certainty, so is something i would personally try. Commented Nov 6, 2016 at 21:59
  • 1
    andidittrich.de/2015/09/… Commented Nov 6, 2016 at 23:05

1 Answer 1

4

Source: PHP: Worry about some magical added “Cache-Control” Header ?

These headers are automatically set by the PHP Session module to prevent browser/proxy based caching of your pages. Depending on your environment setup, it’s possible to control these headers by using the session_cache_limiter() function or use the php.ini

To disable these behaviour just pass an empty string to the session_cache_limiter() function as mentioned in the documentation:

session_cache_limiter('');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.