Use php variables in css

3

I have a problem. I am saving php variables in a database and I want to use those variables in a css file for when I load it with html use those stored values. Normally these variables are hexadecimal codes.

In css I can load these variables from the database via php and give them a value, for example:

In the database I have a variable $ color1 = #ffffff In my css
div {  color: $ color1 }

Is this possible? How would it be done otherwise?

    
asked by Francisco Javier Moivas Rodrig 06.02.2017 в 09:41
source

3 answers

4

Of course it is possible. The simplest way would be to have your CSS in a PHP file that you would link directly as a normal style sheet. For example:

css.php

<?php 
    $azul = "blue"; 
?>
p {
    color: <?php echo $azul; ?>;
    margin:50px;
}

index.php

<link rel="stylesheet" href="./css.php" />
<p>Azul</p>

Although that will work, it would not be entirely correct because the server would be returning PHP instead of CSS (which could lead to some kind of compatibility problems). To avoid this, you should add a line to the beginning of css.php to indicate that the content returned is CSS:

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

With that you already get what you are looking for but, as in the television commercials: " there is still more! ". That works, yes; but users can see that you are serving CSS through PHP (because they can see the extension of the file) and it is information that they do not need to know (the less information users and "bad guys" have about the architecture of your website , better).

Therefore, as a last step (not necessary), I would recommend that you update the redirection to mask css.php as if it were a real .css file. Something like this in .htaccess :

RewriteEngine On
RewriteRule css.css css.php [L]

Then your PHP will be used as CSS, and in addition the users will see it as if it were a real CSS file (although internally it is PHP).

Thus, the two initial files, plus the change in .htaccess indicated above, would look like this:

css.php

<?php 
    header('Content-type: text/css');
    $azul = "blue"; 
?>
p {
    color: <?php echo $azul; ?>;
    margin:50px;
}

index.php

<link rel="stylesheet" href="./css.css" />
<p>Azul</p>

.htaccess

RewriteEngine On
RewriteRule css.css css.php [L]
    
answered by 06.02.2017 в 16:00
0

For what you say just like that, you can only use online styles, for example:

<span style="background-color:<?php $color1; ?>">Texto que sea</span>
    
answered by 06.02.2017 в 09:46
0

Maybe the case is very specific, but my idea in this regard would be to create style sheets as themes for the web, and store the name of the theme, that way when loading the web, then in the header it would complete the path of the css files based on the selected or enabled theme, either for a user or for the public.

$temaActivo would load the current theme from the BD.

<link rel="stylesheet" href="ruta/temas/<?php echo $temaActivo; ?>" />
    
answered by 06.02.2017 в 20:32