Dynamic CSS files with PHP Headers

Published by Subhas Fagu on 19/02/09 16:47:26
Last edited on 26/04/09 01:04:23

Based on the PHP headers article, Lets write a simple dynamic css file that will change the background to blue if it is monday.

First you would have the following php snippet
header('Content-Type: text/css'); // this tells apache or web server to output the following as a css txt file
Proceed to write css as you usually would.

<?php
 header('Content-Type: text/css'); // this tells apache or web server to output the following as a css txt file
?>

body
{
 <?php
if (date('U') == 'Mon')
{
 echo 'background-color:blue;';
}
else
{
 echo 'background-color:white;';
}
?>
 font-size:10px;
}

First thing you will notice is that our file is called screen.php instead of screen.css. This is so when the browser calls the file, apache or IIS knows to render the file against a PHP parser.
Now all we need to do is include it into our html file.

Index.html

<link href="screen.php" rel="stylesheet" type="text/css" media="screen,projection" />
About the Author

Subhas Fagu is an energetic person who you will never catch doing the same thing twice…unless it’s working. He’s always engaged in new concepts, frameworks, operating systems, applications, you name it and you’ve got a conversation. Being a founder of Techlicity Ventures, he's been a key in the business and technical development of all previous and upcoming projects. Subhas contributes a wide variety of perspectives to all projects to ensure risk coverage and smooth deployment.

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!