Other dynamic files built with php

Author: George Hayes
Published:

This article assumes you have a good understanding of PHP. The primary purpose of the tutorial is to provide a few examples of files that can be created with php rather than using a static file. The purpose of this tutorial is not to give you a full working copy of each type of file.

Dynamic CSS files can be useful if you only want to serve what is exactly needed for each page. There are trade offs though for doing this. More time is used to generate the file that simply serving a single static file. Also if you use a single static file for your site your pages will not have to reload the file over and over again. If however you use a different file for each page then it will have that additional load time. If you are trying to reduce serve load and response on a heavy traffic site it could very well pay to use a single static file. That said below is the basics of the php file you will also need to use a Rewriterule in your .htaccess to change the name to have the css ending.

CSS file example
<?php
header('Content-type: text/css');
$css = "";
/*
Load your CSS content here
*/
echo ($css);
?>

Sitemaps and other XML files can be created dynamically as well. A sitemap is probably one of the best non-html pages to create dynamically on a large site. It saves the time it would take to continually update such a file every time new content is created. As with the CSS file a rewriterule in .htaccess will be needed to change the file ending. You can find a more descriptive example of dynamic sitemaps here dynamic sitemaps

sitemap.XML file example
<?php
header("Content-type: text/xml");
$xml = '';
/*
Your XML code here
*/
echo($xml);
?>

Dynamic Javascript files created with PHP can have both positive and negative trade offs depending on what you want to do. If your pages very heavily use javascript it could allow you to create JS files that are customized per each page. It also can have the same effect of dynamic CSS files where it may work out better to load the single large file. Only testing will tell. As you can guess to get the correct file ending you will need a rewriterule in your .htaccess. In fact I'll go ahead and say it now pretty much every time you create a dynamic file using php you will need to do that.

Javascript file example
<?php
header('Content-type: text/javascript');
$js='';
/*
JS code filled in here
*/
echo($js);
?>

Even plain text files such as robots.txt are capable of being written with php. One use would be instead of writing code for every webcrawler you could have it look at the crawler and write the disallow set for it rather than writing one for each crawler.

robots.txt and text file example
<?php
header('Content-type: text/plain');
$text='';
/*
Your text filled in here
*/
echo($text);
?>

Another issue one may consider is that these pages can be cached on the server side as well and served similar to that of a static page. It really should come down to what works best testing. One should always consider performance from the end users side as well as the server load.