Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cache header override

I've been through over 100 answers here, lots to try, NOTHING working??

Have a PHP based site. I need caching OFF for all .php files EXCEPT A SELECT FEW.

So, in .htaccess, I have the following:

ExpiresActive On
# Eliminate caching for certain dynamic files
<FilesMatch "\.(php|cgi|pl)$">
ExpiresDefault A0
Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

Using Firebug, I see the following:

Cache-Control   no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform
Connection  Keep-Alive
Content-Type    text/html
Date    Sun, 02 Sep 2012 19:22:27 GMT
Expires Sun, 02 Sep 2012 19:22:27 GMT
Keep-Alive  timeout=3, max=100
Pragma  no-cache
Server  Apache
Transfer-Encoding   chunked
X-Powered-By    PHP/5.2.17

Hey, Looks great!

BUT, I have a couple .php pages I need some very short caching on.

I thought the simple answer was having this added to the very top of each php page in which I want caching enabled:

<?php header("Cache-Control: max-age=360"); ?>

Nope.

Then I tried various versions of the above. Nope.

Then I tried meta http-equiv variations. Nope.

Then I tried variations of the .htaccess code along with the above variations, such as limiting it to:

# Eliminate caching for certain dynamic files
<FilesMatch "\.(php|cgi|pl)$">
Header set Cache-Control "no-cache, max-age=0"
</FilesMatch>

Nope.

It seems nothing I do will allow a single .php to be cache enabled with the .htaccess code in place, short of removing the statements from the .htaccess file altogether.

Where am I going wrong? What do I have to do to get individual php pages to be cacheable while the rest remain off??

Thank you for any thoughts.

like image 529
Soyo Avatar asked Sep 02 '12 19:09

Soyo


People also ask

How to Set Cache-Control header in PHP?

Setting the "Cache-Control" header is direct. Just use the required directives for "Cache-Control" and send the header through the header function. header('Cache-Control: max-age=86400'); Please note that you must use this function before any output from the script is emitted.

What is nocache in PHP?

The cache limiter defines which cache control HTTP headers are sent to the client. These headers determine the rules by which the page content may be cached by the client and intermediate proxies. Setting the cache limiter to nocache disallows any client/proxy caching.

How do I change my Cache-Control max age?

Cache-Control: max-age=<seconds> This directive tells the browser or intermediary cache how long the response can be used from the time it was requested. A max-age of 3600 means the response can be used for the next 60 minutes before it needs to fetch a new response from the origin server.


2 Answers

Well, apparently this has no answer. So, my solution at this point is to eliminate the .htaccess code altogether, and apply explicit headers to each file. A pain in the you-know-what but it's time to move on. If anyone has a more elegant solution that can work with an .htaccess default please feel free to share... thanks

like image 182
Soyo Avatar answered Oct 29 '22 13:10

Soyo


So I know I'm late.. maybe too late. but I came across a simular problem, and I thought I'd share my solution.

Basically, I turned ExpiresActive Off for every file I didn't want to be cached (or have different caching times than my static resources). It looked something like this:

ExpiresActive On

<FilesMatch "\.(php|cgi|pl)$">

  # This makes sure that no cache headers can be set,
  # but does not generate an error when trying to.
  ExpiresActive Off

</FilesMatch>

# Your normal caching here
ExpiresDefault "access plus 1 month"

Now over in your PHP script, you should be able to insert caching headers without them being overwritten by your .htaccess file, just like you were doing

<?php header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 360) . ' GMT'); ?>

Hope this was helpfull.

like image 24
Max Avatar answered Oct 29 '22 13:10

Max