Forum Moderators: coopster

Message Too Old, No Replies

Making the server treat CSS files like PHP

Without loosing some browsers ability to read the CSS

         

Jeremy_H

5:24 pm on Mar 12, 2006 (gmt 0)

10+ Year Member



Hello,

Does anyone know if it is possible through the .htaccess file to make the server treat the .css files like .php files, while at the same time, maintaining the .css expectations of the visitors browsers?

I was thinking of the benefits of being able to use the dynamic benefits of PHP integrated with the layout power of an external CSS document.

chriswragg

5:47 pm on Mar 12, 2006 (gmt 0)

10+ Year Member



Add the following line into your .htaccess so that your css documents parse as php:
AddType application/x-httpd-php .css

You should then be ok to use the css files as normal in the <link> tag, however if you are having prolems then add the following function into your css files to set the content type:

header('Content-type: text/css');

However adding the line to your .htaccess will mean that all of your css files will be parsed as php, whether dynamic or not, and so may slow down page load.

Chris

Jeremy_H

5:57 pm on Mar 12, 2006 (gmt 0)

10+ Year Member



Thanks so much Chris!

I only have one external CSS document for the site, the rest of them being processed is a moot point, at least for me. However, you have me worried, when you talk about slowing it down. Would you know if this a negligible amount of time, or if it would be noticeable?

Thanks again.

chriswragg

6:13 pm on Mar 12, 2006 (gmt 0)

10+ Year Member



Most likely it wouldn't be noticeable, its just the length of time it would take for the php to read parse and the send the file to the browser. This will of course depend on the functions and the length of the script and if you are connecting to a database etc... This time would probably be less than a second if its just a plain text file with just css in, so its not much of a big deal. But as I said, the time will increase the more PHP you add in.

Chris

jatar_k

7:28 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could try this thread from the CSS Library [webmasterworld.com]
Server Side Scripting in CSS Files [webmasterworld.com]