PHP parsed css file that does not work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

PHP parsed css file that does not work

Post by flycast »

I am experimenting with using PHP to replace bits of data in css files. The idea goes like this. Assign a server base value in a config file. In the css file have php code that echos the server base at the beginning
config.php:

Code: Select all

<?
$base = "http://www.mydomain.com";
?>
structure.css:

Code: Select all

.myclass{
background-image: url(<? echo $base; ?>/images/image.jpg);
}
I have set up the server to parse css type files with this in my .htaccess:

Code: Select all

AddHandler php-script .css
The css ends up looking like this:

Code: Select all

.myclass{
background-image: url(http://www.mydomain.com/images/image.jpg);
}
like I would expect. There is one little problem however, all the CRLF have been replaced with LF. This makes Firefox choke on the css. I am looking for a setting or technique to make sure that the result of the PHP parsing my structure.css is CRLF being preserved.

Ideas anyone?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your results sound strange. It doesn't sound like Firefox, nor PHP. Are you setting the content-type of the resulting css file to be css? PHP's default is to specify HTML.
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

Post by flycast »

It is strange. If you mean:

Code: Select all

<link rel="stylesheet" type="text/css" href="./css/structure.css">
then yes.

I hve looked at the css file with a hex editor. I have a local version that works. When compared to the one that actually comes from the server the only difference is the end of lines. I have web developer plug in installed in Firefox. When I choose to edit the css file in web developer the page displays correctly.

What do you mean
PHP's default is to specify HTML
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Under default configuration, PHP will send a content-type response header signaling the data is HTML. When processing other files, PHP needs to send the proper content-type for that file.

header() should be of interest.
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

Post by flycast »

feyd:

My hat is off to you on this one, that worked!
I simply changed my CSS file to include the PHP header function and it worked.

Code: Select all

<? header("Content-type: text/css; charset=UTF-8"); ?>
img{
<? echo "border: solid red 1px;"; ?>
}
It is important to not output anything before the header command or it will bomb so the header needs to be at the top without any spaces or other code.

Thank you feyd. Have a good day.
Post Reply