1

To have my project respond to certain contexts of content, I want my CSS files to respond respectively instead of setting up additional CSS files (adds to http requests) or parsing the CSS right inside the respective php script (imho: quite messy).

CSS thru PHP parser

Now I could tell the PHP parser via .htaccess to treat css files too.
Disadvantages would be: Overhead of processing several css files in my current project-structure OR breaking structure to activate parsing of CSS files just in one subdirectory.
Either way I simply could do it like so:

AddType application/x-httpd-php php php4 php3 css

... vs. PHP scripts as CSS ressource

Or I could link a php source in the page metadata as css source.
Possible disadvantages: May be some browsers won't accept files with different extensions as CSS ressource...?
Anyways...:

<link rel="stylesheet" type="text/css" href="/style/preprocessed.css.php" media="all">

What would you suggest?

1
  • 1
    Just as long as your preprocessed.css.php file contains valid CSS, I don't see a problem; I've used that approach myself before. Commented Jun 18, 2014 at 16:19

2 Answers 2

1

<link rel="stylesheet" href="style.php"> should work fine in all browsers, but make sure to set the correct header in your PHP file:

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

I wouldn't go parsing CSS files as PHP, otherwise you could do some dangerous stuff:

/* bad.css */
<?php unlink('index.php'); ?>

...or whatever. That shouldn't be able to happen (and other people working on the project may not expect it).

Ideally, you just process all your CSS beforehand and link to a minimized, regular CSS file.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanksalot! Setting the proper header was something out of my league of thinking about such requests, seems it adds to a good solution. Minimizing css beforehand (in this case: on the fly) would be the next step, then. More research grmpf :D
@wesley murch : wrong css is a greta shortcut for administering servers when you don'ty have nintendo hooked up for remote desktop administrationism you've been banned from this comment section <?actionscrip?>>alert('bannd');<?/actionscript?>
@stormdrain doesn not work on nentendo 3ds browser :((((( canu help? var MY_CLICK_BUTTON_3=window.document.getElementById('#MY_CLICK_BUTTON 1); document.body.onClick = function ( ) [ if (alert('please entner youre log in info')) == true (body.refresh.ajaxLoad('invlad logen')) }
@WesleyMurch oh I see you see can we see thr problem is vlad is always fighting logen when drunk so what you have t do is 1) hide the vodka 2) alert on thursdays not wendsdays and 4) when you #MY)CLICK)BUTTON you have to speel out 1 like one k? ther you go
@stormdrain it work! Thanksalot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1

So long as the resource returns Content-Type: text/css browsers will accept it as CSS.

If you want to reduce HTTP traffic use a tool like YSlow too analyze your website. It gives good solid tips on how to optimize your HTTP traffic.

There are 2 relatively easy optimizations you can do:

  1. Enable gzip compression. This reduces the filesize and typically decreases load times.
  2. Set proper caching headers. CSS files are typically static, they don't change from one request to another. I personally like fingerprinting as a caching strategy for resource files like CSS and JavaScript.

There are advantages to letting PHP handle CSS generation but if you can let Apache server the files, all the better. If you do want to serve them with PHP look at the readfile function. It allows you to read and echo a file with very low overhead.


You use the word parsing. I don't think you actually mean parsing but something like serving.

1 Comment

Yes, gzip runs and the headers are some kind of insurance to be "understood just the right way" ;) And now I will have a look at readfile. (YEAH! MORE RESEARCH! :D)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.