tags:

views:

313

answers:

4

I have created css page called style.php and included this the top:

<?php header("Content-type: text/css"); ?>

Does this make you cringe. Is it a terrible idea? I am doing this because I have created a CMS that allows the admin to control colors on pages (so the style.php script queries the database and grabs the hex values).

Any thoughts?

+3  A: 

This is a fine solution, just make sure that you are serving up the appropriate headers. See my blogpost about a related topic (search for "The important headers are" to get to the right section).

One more thing:

With the caching you might get into the situation where the user changes the color she wants to see, but (because it is cached at the client), the page doesn't update. To invalidate the cache, append a ?=id at the end of the URL, where ID is a number that is stored for the user (for example in the session) and is incremented every time she changes the color scheme.

Example:

Cd-MaN
Actually, the proper way of controlling caching is by using the Etag header, not a query string.
David Zaslavsky
Actually, using the query string is a neat trick to enable caching AND control when the cache expires. See http://developer.yahoo.com/performance/rules.html#expires for more information.
Jesse Weigert
Yes, this is not the "proper RFC way", but it is easier to implement (and to get the implementation right) than ETags or If-Modified-Since headers.
Cd-MaN
+1  A: 

Assuming you use appropriate caching, as I imagine the CMS-driven values will probably not change very often, there's no specific reason to avoid creating a CSS include on the fly.

Rex M
+1  A: 

This is not a bad idea. This is a creative idea with numerous benefits:

  • your users can define values w/o you needing to worry about security (parsing css is hard)
  • you can enforce a more visually consistent set of skins (some flexibility is better than total flexibility)
  • simple to code
lucas-insasho
+5  A: 

It's not a bad idea (subject to the notes about caching + content-type), but think about the cost of firing up a PHP instance (mod_php) or passing the script to an already running php (fastcgi style). Do you really want that overhead?

You might be better off writing a "cached" version of your CSS page to a static file, and serving that (or if you need per-page flexibility, selecting which style sheet to include; I assume your main page is PHP already)

Mikeage
+1 good point. See comments of http://stackoverflow.com/questions/487737. 8 down, 7 to go (1 per day) – VonC
VonC