Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cache headers for dynamic css (generated via PHP)

My CSS file is acutally a PHP file which is served with content-type text/css so that I can use PHP variables in that file. style.php looks like this:

<?php
header('Content-Type: text/css');
$bgColor = '#000';
?>

body { background:<?php print $bgColor; ?>; }

It works as expected, but I am a bit worried if the browser caches the dynamically created css file.

When looking at the requests in firebug, it seems to me that the browser is loading style.php anew everytime I reload the page.

I already tried to add these cache headers:

header('Cache-control: must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 60 * 60 * 24) . ' GMT');

But no luck. The file is still loaded everytime the page is loaded. What are the appropriate headers in order to force the browser to cache the file for a certain amount of time?

like image 653
Max Avatar asked Mar 07 '10 08:03

Max


1 Answers

If you want a file to be cached by browsers, you should set the Cache-control header to public:

header('Cache-control: public');

must-revalidate means that the browser will check to see if the file has been updated, which will invoke your PHP script.

like image 85
Inspire Avatar answered Oct 20 '22 12:10

Inspire