In order to solve this issue, you have to consider several things.
First of all, for dynamic content the page where you need to set the headers may not necessarily be the page you think. Here's an example.
Say you have a PHP script, which includes another PHP script. The
included page is what you and the user sees, however, as far as the
browser is concerned the request came from that parent PHP file. In my
case, I was trying to set the headers in included.php
, rather than
parent.php
.
The second consideration is if you are using sessions. If you need to cache the session data as well as the page content, you will need to use session_cache_limiter()
to set the correct headers before you start the session on that page.
My issues became apparent when I used the browser developer tools to see the network request and looked through the headers. Even though I was setting them and specifying an expiration (max-age), the browser ignored it.

But, even after setting the correct cache-control headers, and seeing them in the developer tools the page still was not being cached. How could this be!?! I'll tell you why, notice that Pragma: "no-cache"
in those headers? That's why! We need to change it.
header("Cache-Control: max-age=5600, private_no_expire");
header("Pragma: cache");
require_once 'included.php';

That looks better. Now with any luck, your page will be caching.