Database & PHP & Programming Wabler on 05 Dec 2006 08:03 pm
Dynamic CSS files
Normally .css files are static, meaning, you create them once, and they stay that way, till you download them and rewrite them.
Now, with my solution below, you can make a css file that is completely dynamic. You can load just about everything you want into the file, and it will work just like a normal css file, because when the browser looks at it, the css file is just a css file.
The trick is to not do alot of processing on the css file, because the longer it takes to create it, the longer it will take your page to load (untill of course, your browser caches the css file). You can however drop(or include) a mysql_connect into the file, and pull values out of your database to populate your css file.
Note: The only thing that I have noticed that doesn’t work on my server inside of a .css file, are the PHP ’system’ commands.
The overall idea is simple, trick the server to treat the .css file as a php file.
So…go to your .htaccess file, either through your FTP/SFTP program (view dot files). Or use ‘ls -a’ in your ssh/command line program.
if you dont have an htaccess file, create one, either through command line, or by uploading it.
The string you need as the first two lines of your .htaccess file:
AddType application/x-httpd-php .html .php .css
Options Indexes FollowSymLinks Includes ExecCGI
Now, the “ExecCGI” may or may not be needed on your server, this depends on whether you are running PHP as a perl module or not.
Note: This will also allow you to run your .html files as a PHP file as well.
If you think about it, you can actually add just about any file extension into that first line, to make your server run it as PHP, examples include .yourmom, .is, .ugly, .or, .whatever
Mess with your users, embed PHP wherever you want, have fun!
Sphere: Related Content

















































