Forum Moderators: coopster

Message Too Old, No Replies

Execute PHP in a CSS file?

Any way possible if AddType application/x-httpd-php not set?

         

JAB Creations

6:11 pm on Oct 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On Apache with PHP is there any way to execute PHP code inside of a CSS file? This is assuming AddType application/x-httpd-php does not list CSS as an extension. I would like to serve my stylesheets as CSS but also dynamically construct the CSS for various browsers (and ultimately hope to keep the number of served stylesheets to one).

- John

sned

6:17 pm on Oct 16, 2006 (gmt 0)

10+ Year Member



Funny, I was just fooling around with this the other day:

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

Then name include your css file:
<link rel="stylesheet" href="css.php" />

-sned

Little_G

6:18 pm on Oct 16, 2006 (gmt 0)

10+ Year Member



Hi,

I don't think most browsers care about the file extension so you should be able to just send the correct content-type header and it will work, i.e:

header("Content-Type: text/css");

Andrew

>>damn to slow

[edited by: Little_G at 6:19 pm (utc) on Oct. 16, 2006]

Psychopsia

6:26 pm on Oct 16, 2006 (gmt 0)

10+ Year Member



I use dynamic CSS with PHP and Apache RewriteRule:

RewriteEngine On
RewriteRule ^default\.css$ dir/css.php [nc]

And the same headers, as in the previous post.

jatar_k

6:57 pm on Oct 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this thread form the CSS Library [webmasterworld.com]

Server Side Scripting in CSS Files [webmasterworld.com]

JAB Creations

7:30 am on Oct 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



httpd.conf

Before...

#ScriptAlias /php/ "C:/MEDIA/INTERNET/xampp/php/"
#Action application/x-httpd-php "/php/php-cgi.exe"
LoadModule php5_module "C:/MEDIA/INTERNET/xampp/apache/bin/php5apache2.dll"
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php .php5 .php4 .php3 .phtml

After...

#ScriptAlias /php/ "C:/MEDIA/INTERNET/xampp/php/"
#Action application/x-httpd-php "/php/php-cgi.exe"
LoadModule php5_module "C:/MEDIA/INTERNET/xampp/apache/bin/php5apache2.dll"
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php .php5 .php4 .php3 .phtml .css

To test whether PHP will execute inside a CSS file try to echo some test. If you see the PHP in your browser then your server does not support PHP inside of CSS files.

- John