tags:

views:

81

answers:

7

i want to include my css/stylesheet via php so that...

<link rel="stylesheet" href="http://www.mydomain.com/css/style.php"&gt;

so that i can than dynamicly change different stylesheets.... how can i do that

A: 

In style.php:

echo file_get_contents('style.css');

This will just output the contents of style.css.

Mewp
A: 

why don't you do it the other way around and just include a different css file in your php?

print "<link rel='stylesheet' href='$path_to_css_file'>";
+3  A: 

As long as you set your MIME type in style.php to CSS, you should be in business. Add this to the very top:

<?php Header ("Content-type: text/css; charset=utf-8");?> 

Another option if you're running on an Apache server is to tell it to check .css files for PHP. Add this to your .htaccess file to do this:

AddType application/x-httpd-php .css 

Then you could simply include a regular .css file:

<link rel="stylesheet" href="http://www.mydomain.com/css/style.css"&gt;
Pat
i didnot understant if i have setup all these things then why should i include style.css instead of style.php
Web Worm
Don’t forget to specify the character set.
Gumbo
@testkhan the second example was just showing you how you could get your Apache web server to check .css files for PHP. However setting the correct MIME type in your `style.php` should allow your original code example to work. I've updated my first example with Gumbo's suggestion of setting the charset at the same time.
Pat
Making .css files parsed as PHP doesn't look much of good idea, in my opinion. +1 for the rest of the answer. Spot On.
mirnazim
A: 

You can add this php code in your html head section but file should be .php.

For example: index.php

<html>
  <head>
   <?php

     $cssFile = "style.css";
     echo "<link rel='stylesheet' href='" . $cssFile . "'>";

   ?>
   </head>
   <body>
    ...
    ...
   </body>
</html>

You can store any css file path in $cssFile variable using different conditions.

NAVEED
A: 

Or you may specify link better:

<link rel="stylesheet" type="text/css" href="http://www.mydomain.com/css/style.php"&gt;

But isn't good to have preprocessor for styles, rather use anything like this:

$style;
switch($page_type){
   case "home":$style="/home.css";break;
   case "about":$style="/about.css";break;
}
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$style."\" />";

If user want to style pages more dynamically then do it with javascript, why use server resources and user resources keep unused?

Miro
A: 

You can directly include CSS into a HTML file:

<style type="text/css">
<?php include 'stylesheet.php'; ?>
</style>
Yorirou
A: 

Another variation of dynamically changing the page style:

<link rel="stylesheet" href="css/<?php echo $user['style']; ?>.css">
spidEY