15

I'd like to wrap a CSS file in PHP... So I write the header for the CSS file and give it a .php file extension, thus... css.php.

Will this work if the page is already being used as an include? Or will this new header clash with the frame the page is being included into?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user154107
  • 899
  • 3
  • 10
  • 15
  • Might I ask what you want to use this `css.php` file to serve? Some sort of styles that are database generated or something? – gnarf Sep 19 '09 at 01:00
  • Can you show some code? I'm kinda lost as to what the situation is. – strager Sep 19 '09 at 01:07

5 Answers5

21

gnarf nailed it.

I do:

<link rel="stylesheet" type="text/css" media="screen" href="<? echo TMPL ?>css/name-of-file.css.php">

and then in top of your .css.php file:

<?
header('Content-Type: text/css');
// print out your php-driven css...
?>
Bakudan
  • 19,134
  • 9
  • 53
  • 73
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
  • 2
    You can also use `header("Content-type: text/css; charset: UTF-8");` – Omar Apr 18 '12 at 23:27
  • Make sure that header is printed at very top of the file. If you have (like I did) an empty line at top it may not work somewhere! – MilanG Jun 03 '16 at 08:20
15

If you have a file called css.php just make sure the first lines set the proper content-type header. You may also want to split your session setup stuff (if there is any) into a bootstrap.php if you haven't already. A quick example of loading some style info from a databse:

<?php 
  header("Content-Type: text/css"); 
  include('bootstrap.php');
  // fetch some information to print out our styles
  $result = mysql_query("SELECT * FROM site_styles");
  while ($row = mysql_fetch_assoc($result)) {
    echo $row["selector"]." {\n".$row["css"]."\n}\n";
  }
?>

From your other php file, just output the tag to include the css.php, you do not want to use the php include() function for this task!

<link rel="stylesheet" type="text/css" href="css.php"/>

Although since most browsers will cache your css file pretty aggressively, you might find that dynamically changing the contents of that file doesn't do much good. You could force that to update by adding a get parameter to the href of the link like so:

<link rel="stylesheet" type="text/css" href="css.php?<?php echo $cssversion ?>"/>

Although this is going to completely reload your css file every time that parameter changes. It is generally better practice to serve up static css files for this reason. If you have some styles that need to be loaded from configuration parameters, etc, that don't change very often, the first example should work for you quite well.

gnarf
  • 105,192
  • 25
  • 127
  • 161
8

HTML file:
<link rel="stylesheet" type="text/css" href="css.php"/>

css.php file:
<?php
header("Content-type: text/css; charset=utf-8");
//your php + css code goes here
?>

If your FIRST line of code in your CSS.php file is not
header("Content-type: text/css; charset=utf-8"); it will not work

Omar
  • 11,783
  • 21
  • 84
  • 114
1
#-------------------#
# FlyKit Mod v1.0   # By Ernest Buffington
#-------------------#
    
# put this in your MAIN file
function addPHPCSSToHead($content, $type='file'){
            global $headPHPCSS;
            if (($type == 'file') && (is_array($headPHPCSS) && count($headPHPCSS) > 0) && (in_array(array($type, $content), $headPHPCSS))) 
            return;
            $headPHPCSS[] = array($type, $content);
            return;
        }

# This is loaded in/from your header

    function load_this_from_your_header() 
    {
        global $headPHPCSS;
        
        if (is_array($headPHPCSS) && count($headPHPCSS) > 0) 
        {
            foreach($headPHPCSS AS $php) 
            {
                if ($php[0]=='file') 
                {
                    echo "<style type=\"text/css\">\n";
                    include($php[1]);
                    echo "</style>\n";
                } 
                else 
                {
                    echo "<style type=\"text/css\">\n";
                    include($php[1]);
                    echo "</style>\n"; 
                }
            }
        }
        return;
    }

# Use this to load the PHP CSS in your theme for fly editing.

    addPHPCSSToHead(some_dir.'somefile.php','file'); 

This allows you to edit CSS on the fly and the browser will not cache the changes. 
This is great for designing Themes etc.    
0

It will works if the includer script do not send any output, otherwise you will have an "Headers already sent" error.

using ob_start() in the includer can be a trick to avoid this and have everything work.

Html frames have nothing to do with php includes instead, so no conflict at all in this case.

drAlberT
  • 22,059
  • 5
  • 34
  • 40