动态css的缓存头(通过PHP生成) [英] cache headers for dynamic css (generated via PHP)

查看:138
本文介绍了动态css的缓存头(通过PHP生成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CSS文件是一个可以使用内容类型 text / css 的PHP文件,以便我可以在该文件中使用PHP变量。 style.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; ?>; }

它按预期工作,但我有点担心,如果浏览器缓存动态创建的css文件。

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

当看到firebug中的请求时,在我看来,浏览器每次都加载 style.php 我重新加载页面。

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?

推荐答案

如果你想要的话,可以使用什么合适的标题来强制浏览器缓存文件?一个要由浏览器缓存的文件,你应该将Cache-control头设置为public:

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

header('Cache-control: public');

必须重新验证意味着浏览器将检查文件是否已更新,您的PHP脚本。

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

这篇关于动态css的缓存头(通过PHP生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆