Skip to content Skip to sidebar Skip to footer

How Do I Set A Variable In A Php Page, Pass It To A Css File And Assign It To A Single Css Attribute?

First off, FYI: The .php file referenced below will be the index.php of a Serindipity blog, which is a very complex mess of code and files. In my index.php file, I want to grab the

Solution 1:

I'm not familiar with the engine you mentioned (Serindipity), but this should be pretty straightforward, as long as you are allowed to create and edit your own Php-scripts (which really shouldn't matter as long the engine isn't dependent on the name of the style-sheet):

Change the file-ending of the style-sheet to

.php

and the webserver will recognize it as a php-script. Set the mime-type to text/css like @Brighty wrote

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

and the user will never know the difference (except, of course, the file-ending being .php).

If the style-sheet having a file-ending of .php is a problem, you can always use .htaccess and some clever regex to make the user believe they are seeing a .css.

Edit: Also, if you have access to configure what file/s will be recognized as php-scripts, you can set your server to also recognize .css as a php-file (which, as far as I know wouldn't be a problem, since php will just toss any plain text encountered out to the user).

Solution 2:

Just make sure your file is a .php file and you can just use PHP in it like so:

<?php$colour1 = isset($variable) ? $variable : '#000';

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

body {
    background: <?phpecho$colour1; ?>
}

div {
    color: <?phpecho$colour1; ?>
}

Obviously this is quite flexible! Here's a link I found extending the method even more.

Solution 3:

you can't set any variable in the css file, jquery or javascript is the best option to play with your css file.

or you will do with inline css.

Solution 4:

You may be able to achieve this by mixing css and php code in a single file with a php extension, and setting the mime type of the file to 'text/css'.

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

I have done a similar thing in ColdFusion but never in php so I can't guarantee this will work.

Post a Comment for "How Do I Set A Variable In A Php Page, Pass It To A Css File And Assign It To A Single Css Attribute?"