I want to create a stylesheet in a PHP file (styles.php), such that the stylesheet becomes dynamic, depending on the user who requests is. For each individual user the stylesheet is constant, and should therefore be cached on his client browser.
I have read that you can achieve this by setting headers for content-type and cache-control and such, but I can't get this to work. Apparently there's more to it, and maybe it is not even possible. Browsers don't always seem to listen to caching headers.
Does anyone know what is required to let a PHP file be cached in the browser?
I don't want to put all the dynamic CSS in a style-block in the HTML, and I don't want to change my Apache configuration for this. If it's truly not possible what I want, I also would like to know. Thanks!
These headers should work fine:
$expires = 60*60*24; // how long to cache in secs..
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
header('Content-type: text/css');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With