PHP variables into CSS?

Discussion in 'PHP' started by zurih, Apr 25, 2010.

  1. #1
    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
     
    zurih, Apr 25, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    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:
     
    danx10, Apr 25, 2010 IP
  3. jaholden

    jaholden Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    jaholden, Apr 29, 2010 IP
  4. zurih

    zurih Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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...
     
    zurih, Apr 29, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    Could he add a handler to his .htacces file ? ...would it work for the original method ?

    AddHandler application/x-httpd-php5 .html .htm .css
     
    Last edited: Apr 29, 2010
    MyVodaFone, Apr 29, 2010 IP
  6. jaholden

    jaholden Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    jaholden, Apr 29, 2010 IP
  7. sevnrock

    sevnrock Peon

    Messages:
    241
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Have you considered creating multiple css files and just using PHP to choose one on the page level?
     
    sevnrock, Apr 29, 2010 IP
  8. Niku01

    Niku01 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Use for stylesheet a .php file, and with mod rewrite make style.css -> your .php file . That is the easiest way. Good lock.
     
    Niku01, Apr 29, 2010 IP
  9. aTo

    aTo Active Member

    Messages:
    473
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    78
    #9
    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" />
     
    aTo, Apr 30, 2010 IP
  10. zurih

    zurih Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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
     
    zurih, May 1, 2010 IP
  11. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #11
    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
     
    MyVodaFone, May 1, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    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):
     
    danx10, May 1, 2010 IP
  13. georgu

    georgu Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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, May 1, 2010 IP
  14. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #14
    @ 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.
     
    danx10, May 2, 2010 IP
  15. georgu

    georgu Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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.
     
    georgu, May 3, 2010 IP