The Little Guy Posted October 31, 2008 Share Posted October 31, 2008 It's very simple. Basic Add this at the beginning of a PHP file <?php header("Content-type: text/css"); ?> under that, place your CSS, (you can also use PHP) in your HTML, place this (change your href to you style): <link rel="stylesheet" type="text/css" href="themes/default.php"> Advanced The second way, is only so your href doesn't contain a php extention. In an .htaccess file place this: Options +FollowSymlinks RewriteEngine On RewriteRule themes/default.css themes/default.php Now your CSS can look like this: <link rel="stylesheet" type="text/css" href="themes/default.css"> basically all it does is redirect the user to the PHP file with out them knowing. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/ Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 Or just configure .css files to be handled by PHP. What is this post for anyway? Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679553 Share on other sites More sharing options...
Mchl Posted October 31, 2008 Share Posted October 31, 2008 Or just configure .css files to be handled by PHP. What is this post for anyway? Fir me it's information I was going to look for soon... Well it came to me by itself. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679569 Share on other sites More sharing options...
Maq Posted October 31, 2008 Share Posted October 31, 2008 Or just configure .css files to be handled by PHP. What is this post for anyway? Fir me it's information I was going to look for soon... Well it came to me by itself. Magical... Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679572 Share on other sites More sharing options...
Mchl Posted October 31, 2008 Share Posted October 31, 2008 Magical... Conspiracy actually... Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679574 Share on other sites More sharing options...
The Little Guy Posted October 31, 2008 Author Share Posted October 31, 2008 What is this post for anyway? Just information I felt like sharing. I would also like to add: <?php $value = 'something'; ?> <link rel="stylesheet" type="text/css" href="themes/default.php"> if you try to use $value in themes/default.php it will not be set. That is the same with "include[_once]", "require[_once]" your css file will not inherit any code from the previous file. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679590 Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 Better check up on your PHP... file1.php <?php $var = 'foo'; ?> file2.php <?php include 'file1.php'; echo $var; // output: foo ?> It's not the same as require and include. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679596 Share on other sites More sharing options...
The Little Guy Posted October 31, 2008 Author Share Posted October 31, 2008 That isn't what I meant, I know that will work, but if you do this: file1.php <?php $var = 'foo'; ?> <link rel="stylesheet" type="text/css" href="themes/default.php"> themes/default.php <?php header("Content-type: text/css"); echo $var; // nothing gets output ?> then in themes/default.php echo $var, nothing will echoed out since it is a separate request to the server. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679605 Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 I don't know what you mean, only what you say. After saying what you said above you said: That is the same with "include[_once]", "require[_once]" Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679606 Share on other sites More sharing options...
The Little Guy Posted October 31, 2008 Author Share Posted October 31, 2008 I don't know what you mean, only what you say. After saying what you said above you said: That is the same with "include[_once]", "require[_once]" OK, try this: file1.php <?php include 'file2.php' ?> <link rel="stylesheet" type="text/css" href="themes/default.php"> file2.php <?php $var = 'foo'; ?> themes/default.php <?php echo $var; ?> Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679608 Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 Dude, I know. In fact it's pretty obvious. But you said it's the same when including files. It isn't. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679610 Share on other sites More sharing options...
The Little Guy Posted October 31, 2008 Author Share Posted October 31, 2008 Its obvious to you because you know PHP, but what about those who don't, they may not have known that. I tried to do that, and like 5 seconds later realized that the CSS file was a separate request to the server, so included files and variables didn't get passed from file1.php to my css file. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679618 Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 HTTP is a stateless protocol. You are making two requests. I.e. obvious. Web developers should have knowledge of the HTTP protocol as well in order to be good developers. If you need to do that then use cookies or sessions. Quote Link to comment https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/#findComment-679622 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.