简体   繁体   中英

Cache PHP page that echoes CSS code

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM