动态css的缓存头(通过PHP生成)

编程入门 行业动态 更新时间:2024-10-10 08:18:15
本文介绍了动态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生成)

本文发布于:2023-11-01 10:59:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   动态   css   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!