I have a PHP 5.1.0 website (actually it's 5.2.9 but it must also run on 5.1.0+).
Pages are generated dynamically but many of them are mostly static. By static I mean the content don't change but the "template" around the content can change over time.
I know they are several cache systems and PHP frameworks already out there, but my host don't have APC or Memcached installed and I'm not using any framework for this particular project.
I want the pages to be cached (I think by default PHP "disallow" cache). So far I'm using:
session_cache_limiter('private'); //Aim at 'public'
session_cache_expire(180);
header("Content-type: $documentMimeType; charset=$documentCharset");
header('Vary: Accept');
header("Content-language: $currentLanguage");
I read many tutorials but I can't find something simple (I know cache is something complex, but I only need some basic stuff).
What are "must" have headers to send to help caching?
This is the best solution for php cache Just use this in the top of the script
I was doing JSON caching at the server coming from Facebook feed nothing was working until I put flush and hid error reporting. I know this is not ideal code, but wanted a quick fix.
This worked very well.
You must have an Expires header. Technically, there are other solutions, but the Expires header is really the best one out there, because it tells the browser to not recheck the page before the expiration date and time and just serve the content from the cache. It works really great!
It is also useful to check for a If-Modified-Since header in the request from the browser. This header is sent when the browser is "unsure" if the content in it's cache is still the right version. If your page is not modified since that time, just send back an HTTP 304 code (Not Modified). Here is an example that sends a 304 code for ten minutes:
You can put this check early on in your code to save server resources.
Take your pick - or use them all! :-)
Here's a small class that does http caching for you. It has a static function called 'Init' that needs 2 parameters, a timestamp of the date that the page (or any other file requested by the browser) was last modified and the maximum age, in seconds, that this page can be held in cache by the browser.
Setting an expiration date for the cached page is one useful way to cache it on the client side.