Let browsers cache my dynamic PHP style sheet

I want to create a stylesheet in a PHP file (styles.php) so that the stylesheet becomes dynamic, depending on the user who is requesting. For each individual user, the stylesheet is permanent and therefore must be cached in its client browser.

I read that you can achieve this by setting headers for the content type and cache management and the like, but I can't get this to work. Apparently there is more, and perhaps this is not even possible. Browsers do not always listen to cache headers.

Does anyone know what is required for a PHP file to be cached in a browser?

I don’t want to put all the dynamic CSS in a style block in HTML, and I don’t want to change my Apache configuration for this. If this is really impossible, what I want, I would also like to know. Thanks!

+6
source share
2 answers

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 MYH:i:s', time()+$expires) . ' GMT'); header('Content-type: text/css'); 
+10
source

seems like it seems please check.

Cache Headers for Dynamic Css

0
source

All Articles