tags:

views:

58

answers:

6

ih ave read about the technique to allow dynamic HTML stylesheets using PHP's preprocessor, however I was windering how safe this tecnique is, and if for any reason it is discouraged.

For example, instead of using typical:

 <link rel="stylesheet" type="text/css" href="http:mysite.com/style.css/>

I could use:

 <link rel="stylesheet" type="text/css" href="http:mysite.com/style.php/>

What do you think?

+5  A: 

It's perfectly fine.

I'd suggest setting the following header in the PHP though:

Header("Content-Type: text/css");

Mikee
Do you see any potential drawback on using this technique?
Riccardo
No, there'll be a very slight speed issue because the file will have to run through the PHP parser first - but the speed impact is not something you'd ever notice. No different to using PHP for serving HTML files.
Mikee
Oh, and if you have an error in your PHP it'll obviously break your styles when the PHP error is outputted. But that is the same for the rest of your site too!
Mikee
+1  A: 

It's only as safe as the code that you put into style.php.

Andrew Sledge
A: 

You can render any kind of Text Output with PHP (and other stuff) including CSS, just make sure the right header is given header('Content-Type: text/css; charset=iso-8859-1'); (or any other charset).

But to be honest I can't think of an Situation were it would be necessary to use a dynamic stylesheet in that manner.

Hannes
Here one: if you use a web test environment like I do, you will end having references to "schemes" sometimes impossible to replicate on both development and production web server, not to mention the case you will be using CDN for static content delivery.A dynamic CSS would help in this scenario...but if you have a better solution I'll be glad to learn! :-)
Riccardo
How do you know they're using iso-8859-1?
ceejayoz
@ceejayzo "or any other charset"
Hannes
+3  A: 

If you can use .htaccess files you can set it up to parse *.css files as PHP using:

AddHandler application/x-httpd-php .css

Then you can use PHP directly within your *.css files.

Be sure to also set the header type to text/css as the others have mentioned as well:

header('Content-type: text/css');
evolve
+1  A: 

Yes, you can use PHP to generate your stylesheet. Make sure to declare the output properly as CSS by sending an appropriate Content-Type value specifying both the media type and the character encoding.

But note that it costs additional time and resources to generate the stylesheet with every request. So you should add some kind of caching mechanism (static files and HTTP caching) to reduce server load and even unnecessary requests.

Gumbo
+1  A: 

As others have mentioned, PHP can be used to output any kind of text. So it's not a problem to output dynamic CSS (or even dynamic JavaScript). Be aware though that you're increasing your server load by doing this. The server will have to fire up the PHP engine to serve what would otherwise be a simple static .css file.

mellowsoon