Jump to content

How to make a dynamic CSS Style Sheet


The Little Guy

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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.