Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/130901-how-to-make-a-dynamic-css-style-sheet/
Share on other sites

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.

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.

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; ?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.