Welcome Guest, Not a member yet? Register   Sign In
PHP in CSS file
#1

[eluser]Kemik[/eluser]
Hey all,

This is a bit of a noob question but if I put php in a css file and load the css file as normal will the php run?

Code:
#middle_banner{
width:651px;
height:165px;
margin:auto;
padding:20px 0 0 0;
background:url(images/middle_banner_2.jpg) no-repeat center;
}

PHP's rand() function would be great to select a random middle_banner. I was going to do
middle_banner_<?php rand(2, 6) ?>.jpg

Thanks.
#2

[eluser]daulex[/eluser]
found this on the net
Quote:I guess php tags were not being processed because the webserver did not associate a .css file with the php script engine.
The simplest way is to save the css file as
stylesheet.php and change the link to

link href="stylesheet.php" rel="stylesheet" type="text/css"

Alternatively you could solve that by telling your webserver that .css files are infact php scripts, but how you do that depends on what webserver you are using. I'd stick to the first method, as it's simple.
#3

[eluser]Tom Glover[/eluser]
[quote author="daulex" date="1213747249"]found this on the net
Quote:I guess php tags were not being processed because the webserver did not associate a .css file with the php script engine.
The simplest way is to save the css file as
stylesheet.php and change the link to

link href="stylesheet.php" rel="stylesheet" type="text/css"

Alternatively you could solve that by telling your webserver that .css files are infact php scripts, but how you do that depends on what webserver you are using. I'd stick to the first method, as it's simple.
[/quote]

Remember php ignores any other code in a file that is not wrapped in php tags.
#4

[eluser]Kemik[/eluser]
I tried changing the extension to php before I opened this topic as it seemed common sence. Apparently you need to add some header and content type info to the top of the css file, which I originally missed.
#5

[eluser]Wilbur Himself[/eluser]
you can do it, you must have the file with a .php extension, and you must set a header like:

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

[eluser]John_Betong[/eluser]
Hi Kemik,
 
>>> PHP’s rand() function would be great to select a random middle_banner.
>>> I was going to do middle_banner_<?php rand(2, 6) ?>.jpg
 
I recently introduced this code so that the 1676x76px smiley banner starts at a different position each time the page is loaded.
Code:
<body>
   ...
   ...
    <?php $X = 20 * rand(0,80) ?>    
    <div style='clear:both; background:transparent url(/afiles/logo/la_1676x72.jpg) -&lt;?php echo $X ?&gt;px 0 repeat-x'>
    </div>  
   ...
   ...
&lt;/body&gt;
&nbsp;
You can see it here
&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB