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?

Amanahumpa
  • 55
  • 12
  • 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. – Funk Forty Niner Jun 18 '14 at 16:19

2 Answers2

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.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • 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 – Amanahumpa Jun 18 '14 at 16:32
  • @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 >alert('bannd');/actionscript?> – stormdrain Apr 27 '17 at 13:58
  • 1
    @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')) }` – Wesley Murch Apr 28 '17 at 00:15
  • 1
    @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 Apr 28 '17 at 16:16
  • @stormdrain it work! Thanksalot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! – Wesley Murch Apr 28 '17 at 21:36
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.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • 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) – Amanahumpa Jun 18 '14 at 16:34