包括:带有 .php 文件扩展名的 CSS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1445424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include: CSS with .php file extension?
提问by user154107
I'd like to wrap a CSS file in PHP... So I write the header for the CSS file and give it a .php file extension, thus... css.php.
我想用 PHP 包装一个 CSS 文件...所以我为 CSS 文件编写标题并给它一个 .php 文件扩展名,因此... css.php。
Will this work if the page is already being used as an include? Or will this new header clash with the frame the page is being included into?
如果页面已经被用作包含,这会起作用吗?或者这个新的标题会与包含页面的框架冲突吗?
回答by virtualeyes
gnarf nailed it.
纳尔夫钉了它。
I do:
我愿意:
<link rel="stylesheet" type="text/css" media="screen" href="<? echo TMPL ?>css/name-of-file.css.php">
and then in top of your .css.php file:
然后在 .css.php 文件的顶部:
<?
header('Content-Type: text/css');
// print out your php-driven css...
?>
回答by gnarf
If you have a file called css.phpjust make sure the first lines set the proper content-type header. You may also want to split your session setup stuff (if there is any) into a bootstrap.phpif you haven't already. A quick example of loading some style info from a databse:
如果您有一个名为的文件,请css.php确保第一行设置了正确的内容类型标题。bootstrap.php如果您还没有,您可能还想将您的会话设置内容(如果有)拆分为一个。从数据库加载一些样式信息的快速示例:
<?php
header("Content-Type: text/css");
include('bootstrap.php');
// fetch some information to print out our styles
$result = mysql_query("SELECT * FROM site_styles");
while ($row = mysql_fetch_assoc($result)) {
echo $row["selector"]." {\n".$row["css"]."\n}\n";
}
?>
From your other php file, just output the tag to include the css.php, you do notwant to use the php include()function for this task!
从您的其他PHP文件,只输出标签包括css.php,你不希望使用PHPinclude()函数完成这个任务!
<link rel="stylesheet" type="text/css" href="css.php"/>
Although since most browsers will cache your css file pretty aggressively, you might find that dynamically changing the contents of that file doesn't do much good. You could force that to update by adding a get parameter to the href of the link like so:
尽管由于大多数浏览器会非常积极地缓存您的 css 文件,您可能会发现动态更改该文件的内容并没有多大好处。您可以通过向链接的 href 添加一个 get 参数来强制更新,如下所示:
<link rel="stylesheet" type="text/css" href="css.php?<?php echo $cssversion ?>"/>
Although this is going to completely reload your css file every time that parameter changes. It is generally better practice to serve up static css files for this reason. If you have some styles that need to be loaded from configuration parameters, etc, that don't change very often, the first example should work for you quite well.
尽管每次参数更改时都会完全重新加载您的 css 文件。由于这个原因,提供静态 css 文件通常是更好的做法。如果您有一些需要从配置参数等加载的样式,并且不经常更改,那么第一个示例应该非常适合您。
回答by Omar
HTML file:<link rel="stylesheet" type="text/css" href="css.php"/>
HTML文件:<link rel="stylesheet" type="text/css" href="css.php"/>
css.php file:<?phpheader("Content-type: text/css; charset=utf-8");//your php + css code goes here?>
css.php 文件:<?phpheader("Content-type: text/css; charset=utf-8");//your php + css code goes here?>
If your FIRST line of code in your CSS.php file is notheader("Content-type: text/css; charset=utf-8");it will not work
如果您的 CSS.php 文件中的第一行代码不是header("Content-type: text/css; charset=utf-8");,它将无法工作
回答by drAlberT
It will works if the includer script do not send any output, otherwise you will have an "Headers already sent" error.
如果包含脚本不发送任何输出,它将起作用,否则您将收到“已发送标头”错误。
using ob_start() in the includer can be a trick to avoid this and have everything work.
在包含器中使用 ob_start() 可以避免这种情况并使一切正常。
Html frames have nothing to do with php includes instead, so no conflict at all in this case.
Html 框架与 php 包含无关,因此在这种情况下根本没有冲突。

