Generate CSS with PHP

Here the files are named pages.css and pages.php.

1, Change pages.css to pages.php;

2, On the HTML file, change pages.css to pages.php as below:
<link rel=”stylesheet” href=”pages.php?path=http://www.coolder.com/” type=”text/css” />

This line tells the browser that the file is CSS.
If you try to open it by putting its address and name to the address space on the browser, it should pop out a “file download” window. The downlaoded file is an .css file. If the content is printed on the browser window, it won’t work.

At the beginning, I opened the pages.css file and saved as the pages.php file. Whatever I changed the script, it always didn’t work. I created a new php file, then it works with the same script as that file built from .css file. 

3. Add this line to the top of the pages.php file:

<?php header(“Content-type: text/css”); ?>

4, put the static or dynamic CSS between “print<<<CSS” and “CSS;

Here is an example:

<?php
header(‘Content-type: text/css’);
$path = $_GET[“path”]
;
print <<<_CSS
@charset “utf-8”;
body {
    margin = 0px;
    background:url(“${path} top.gif”); background-repeat:repeat-x; background-color:#EEF2F2;
    }
img {
    border-width: 0px 0px 0px 0px
    }  
td  {
    font:14px Verdana, Arial, Helvetica, sans-serif;
    line-height:20px;
    color:#003366;
    }
_CSS;

?>

Leave a Reply

Your email address will not be published. Required fields are marked *