Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let browsers cache my dynamic PHP stylesheet

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!

like image 654
myrddin 81 Avatar asked Feb 21 '13 10:02

myrddin 81


1 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 M Y H:i:s', time()+$expires) . ' GMT');
header('Content-type: text/css');
like image 158
Andrew Avatar answered Sep 18 '22 23:09

Andrew