6

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!

myrddin 81
  • 61
  • 1
  • 2
  • what are the factors that influence the stylesheet? is the user able to configure the style sheet? or do you create a random one? – t.niese Feb 21 '13 at 10:30
  • Our system has many modules, and there's a central webservice that returns some styling settings for the logged in user. For me they are random, but obviously the they are related to some user details like the group it belongs to. – myrddin 81 Feb 21 '13 at 10:49
  • so you use php sessions when your style script is called? If that is the case this is most likely your problem. I could not check right now, but if i remember right php overwrite your cache headers by default if you use sessions. So check the headers that the browser receives for the stylesheet. – t.niese Feb 21 '13 at 11:16
  • That's something I didn't think of yet. Thanks! But I did check the headers that the browser receives, and those are the ones I send. So, either the browser decides to ignore them, or the headers are incomplete, or what I'm trying is just not possible. – myrddin 81 Feb 21 '13 at 12:40

2 Answers2

10

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');
Andrew
  • 12,617
  • 1
  • 34
  • 48
  • Was just having exactly the same issue using a dynamic css styleheet in Wordpress and the above fixed it! Thanks :) – ss888 Feb 21 '13 at 16:13
0

this seems to be similar, please check.

Cache Headers for dynamic Css

Community
  • 1
  • 1
Ankit
  • 1,867
  • 2
  • 21
  • 40
  • because it sounds like he wants to differ the stylesheet from user to user it is actually a different question. and then the cache control should not be `public` but `private` (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1) – t.niese Feb 21 '13 at 10:33
  • Yet, I've seen this page and tried it. Unfortunately, the browser keeps retrieving the php stylesheet. – myrddin 81 Feb 21 '13 at 10:43
  • Yes, I did. And I tried to add an 'Expires' header, but with no effect. I tested both in Firefox and in Chrome (latest stable versions) – myrddin 81 Feb 21 '13 at 10:51