Hey, I'm trying to set some CSS values from PHP. The way I tried doing it by defining the PHP variable in the PHP page, for example: $bodyFont = "Arial, Helvetica, Sans-Serif"; Code (markup): and then in the CSS file, write it like this: body { font-family:<?php echo $bodyFont; ?>; } Code (markup): but it doesn't seem to work. Any idea how can I do it? Thanks
It should work, ensure you have pasted the following code within a .php file. <?php $bodyFont = "Arial, Helvetica, Sans-Serif"; ?> <style> body { font-family:<?php echo $bodyFont; ?>; } </style> PHP:
Does your stylesheet have a .css extension or a .php extension? There's nothing that says it has to have a .css extension, but it will only be passed to PHP if it has a .php extension. Remember that the browser will try and cache the stylesheet so if you're enabling people to customise the way they view the site, the changes may not take effect for them immediately if the old version of the stylesheet stays cached in their browser. You could send headers that would make the browser not cache it, but you'd make them pull the stylesheet for each page load which would slow the responsiveness of the site.
Yeah I know that's the only downside I found when using .php instead of .css. I'm still trying to find the best "elegant" way to do this. Shame css can't recognize variables out of the box tbh...
Could he add a handler to his .htacces file ? ...would it work for the original method ? AddHandler application/x-httpd-php5 .html .htm .css
Just a thought... are you trying to customise it on a per-user basis, or just provide a means of having variables in it? If it's just to have variables, you could have your web server parse it as an SSI file (server-side includes). See http://httpd.apache.org/docs/2.2/howto/ssi.html If it's for per-user customisation, you could just have the defaults in the external CSS and have an inline style to do the customisation in the common HTML header in your app.
Use for stylesheet a .php file, and with mod rewrite make style.css -> your .php file . That is the easiest way. Good lock.
try making a .php file and insert this line: <?php header("Content-Type: text/css"); $color = $_POST['color']; //css code(s) here, sample: body{ color: $color; } ?> PHP: just try it out... and try to wander round it... hope it helps... just end the file with a .php file extension this is for external stylesheets... <link rel="stylesheet" type="text/css" href="css/style.php" />
Hmm, I'm trying the htaccess way but it doesn't seem to work I added this to my .htaccess file: AddHandler application/x-httpd-php .css Code (markup): And in my CSS file, I got: body { font-size: <?php echo $fontsize; ?>; font-family: <?php echo $fonts; ?>; color: #000; margin: 0; padding: 0; text-align: center; } Code (markup): These variables are defined at the top of the page. And when I'm viewing the page, the CSS file returns: font-size: ; font-family: ; Code (markup): Any idea? Thanks
You could try AddType instead of AddHandler and if its php5 your working with you need to specify that like so application/x-httpd-php5 .css I don't see any reason for this not to work, its a simple command to tell php to consider a .css file as php
Add to your .htaccess the following line: AddType application/x-httpd-php .php .css Code (markup): Then you'll be able to parse php within all your .css files. So example usage: Create a .css file named style.css, containing the following code: <?php header("Content-Type: text/css"); $bodyFont = "Arial, Helvetica, Sans-Serif"; ?> body { font-family:<?php echo $bodyFont; ?>; } PHP: And call it like: <link rel="stylesheet" type="text/css" href="style.css" /> Code (markup):
You lose the variables because you call it with link rel. This means another request, another process, nothing to do with the page you call from. Consider making themes or include the php file containing your css code in your file using require(). AddType application/x-httpd-php .php .css Code (markup): is not recomended. Try something like this: css.php file: <? body { font-family:<?php echo $bodyFont; ?>; } ?> Code (markup): mainfile.php <? $bodyFont = "Arial"; echo '<style type="text/css">'; require('css.php'); echo '</style>'; //Usefull code ?> Code (markup): Off course, some custom theme system is the most elegant thing to do.
@ georgu I believe, if I understand correctly, the OP wants to have the php variables within the css file, theirfore no need to worry about what you posted.
well then..the only way to have the php variables in a css file that you call using link rel is to call it with the php extension and also pass parameters like this: file.php <?$bodyFont = 'Arial'; ?> <?$bodyColor = '#fff'; ?> <link rel="stylesheet" type="text/css" href="style.php?bodyFont=<?=$bodyFont?>&bodyColor=<?=$bodyColor?>" /> Code (markup): You can also point style.css to style.php in htaccess and use .css extension.