- it requires additional HTTP requests, and
- you can encounter the same-domain connection limit.
Here is how this digital surgery would work for CSS. You need to tell the server two things: first, to parse CSS files for PHP commands and second, to send the correct MIME type:
AddHandler application/x-httpd-php .css header('Content-type: text/css');
Next you can merge your CSS files together with PHP inside the CSS file like this:
<?php include("layout.css"); include("navigation.css"); include("advanced.css"); ?>
To deliver files based on browser environment variables (for example, to simulate an @import to filter out older browsers), you could use software like phpsniff available at Sourceforge phpsniff.
Caching Dynamic Files
As specified above, the dynamic CSS file will not cache properly. If you add the following headers to the top of your PHP file after the content type, they will cache for three hours (adjust 10,800 seconds as necessary).
header('Cache-control: must-revalidate'); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 10800) . ' GMT');
- CSS + PHP: Organized and Optimized?
- Explores the idea of suturing CSS files together with PHP. By Mike Papageorge of Fiftyfoureleven.
- Souders, S., High Performance Web Sites
- (Sebastopol, CA: O’Reilly Media, 2007), 45. Scripts block parallel downloading.
- Minimize HTTP Requests
I found a sweet tool for combining/minifying JS files at http://www.scriptalizer.com. It doesn’t do CSS, but rocks on JS.
Hi, thanks this helps a lot, but where do I add the pieces of code you mentioned? Do I put these in a new CSS file or a php file?
AddHandler application/x-httpd-php .css