Forum Moderators: coopster
Alas, I don't think scripting is allowed in CSS files, or is there some way I don't know of?
I've thought of just putting the CSS in the page itself, thereby getting PHP in on the action. But this seems like a step in the wrong direction.
Just remember to output the proper MIME type at the top of your PHP css file:
<? header('Content-Type: text/css');?> If your generated CSS won't be changing from request to request, I'd also suggest finding and using a recipe for handling conditional "If-Modified-Since" browser requests. Otherwise some browsers will be re-requesting the CSS more often than they need to.
<?php
header('Content-type: text/css');
$col1 = "#CCC";
echo ("
.ex {
color:$col1;
}
#ex2{
bgcolor:$col2;
}
");
?>
Having done that, you can call in the normal way.
<link rel="stylesheet" type="text/css" href="/css.php" />
But remember, php >> css means more work on the server for every page view. Hope it helps.
Zipper's comment on server load is very worthy, however, and I've been considering converting to a script that writes .css flat files instead (it's not like you want CSS files dynamic anyway!). So before becoming too invested, consider that option.
As it is, in the config file I like to define constants rather than setting variables for colors, etc. That way if I need some odd-ball style attribute in a span tag on other pages I can easily pop it in wherever I want.
AddType application/x-httpd-php .css
to my .htaccess file &
header('Content-Type: text/css');
to the top of the php in the .css file...
and it still won't parse the .css file with php. I know this because I requested the .css from the browser and it (both moz & ie) didn't know what to do with it. When I finally saw it, the php was intact, unparsed. Now what?
However I also removed the
AddType application/x-httpd-php .css
from the .htaccess file and it still works. I thought the purpose of the above code was to get php to parse inside css files. Why won't this work if I leave the .css ext on?
What this means to me is that since I have many sites on one server, whenever I want to use php in a .css file, I have to rename it. What I would prefer is to put that code in my .htaccess file and just use php in the .css files as needed (which may be never). What am I doing wrong?
p.s. thanks for the help :)
<VirtualHost ...>
...
AddType application/x-httpd-php .htm .css
...
</VirtualHost> Oh, yeah. At least when you do it that way you have to restart Apache, though.
Here is a quick script I wrote that creates a CSS file (load your CSS from your DB into a string called '$css' first). Make a blank file called 'blank.css'.
<?php
//create file
$filename = "test.css";
$blankfile = "blank.css";
unlink ($filename);
if (!copy($blankfile, $filename))
{
echo "Can't copy!";
}
if (is_writable($filename))
{
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $css) === FALSE)
{
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
?>