1

Following the accepted answer here on SO, I am trying to create a stylesheet that is editable with PHP.

What's happening...

I am trying to make the stylesheet (named css.php) cache in the user's browser so that he/she does not have to load it on every pageload, and have set the following headers to do so:

header('Content-Type: text/css;;charset=UTF-8');
header('cache-control: max-age=86400;');
header('Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT;');
header('Expires: Sat, 22 Nov 2014 16:00:00 GMT;');
header('cache-control: max-age=86400;');

These are the other headers sent by default:

Connection:"Keep-Alive"
Content-Encoding:"gzip"
Content-Length:"393"
Date:"Fri, 21 Nov 2014 17:00:50 GMT"
Keep-Alive:"timeout=5, max=99"
Server:"Apache"
Vary:"Accept-Encoding"
X-Frame-Options:"SAMEORIGIN"

However, upon loading a page that references the css.php page multiple times, it continues to reload the CSS page every time.

How do I know this...

I am receiving a hit to the css.php page every time I load the page which uses the stylesheet on my apache server access logs.

I can see that my Firefox browser is accessing the css.php page in the Inspect Element tool. It is receiving a HTTP 200 every time.

What should I do?

21
  • you need to set headers expires time in advance. You're setting it for today, browsers normally cache the css anyway, the problems normally arise when someone changes the css and the browser has the cached version so the changes don't show up, this is backwards... Commented Nov 21, 2014 at 17:55
  • Today is the 21st. Plus, the CSS is not cached normally here because it is disguised as a PHP file. Commented Nov 21, 2014 at 17:56
  • 1
    Receiving a HTTP 200 is normal even if the item is cached. What you need to look for is (from cache) which is displayed in size/content column in Chrome DevTools (no idea about firefox). Also keep in mind that most devtools disable that cache by default to you may need to untick the disable cache checkbox (in chrome this is at the top of the network panel) Commented Nov 21, 2014 at 17:59
  • @billy Don't worry, I lose days too... :) Commented Nov 21, 2014 at 17:59
  • 1
    when I was writing in the comment box the page was scrolled down so couldn't see the tp box at the same time..note to self, read questions properly.... Commented Nov 21, 2014 at 18:17

1 Answer 1

0

Instead of creating a dynamic CSS file, change the standard one each time with PHP's file_put_contents() function.

Example:

 file_put_contents("styles.css", $css_input);

That way, browsers will cache the file like normal.

Example:

<link rel="stylesheet" type="text/css" href="styles.css"/>

When you make changes the the actual CSS, browsers will automatically load the new one upon restart.

Sign up to request clarification or add additional context in comments.

1 Comment

This is somewhat of a workaround... but gets the job done just the same! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.