CSS Randomizer

This is a little script I wrote which loads a random* CSS file from a directory. Useful if you have multiple header images and wish to rotate them daily.

It works by indexing all the files in a directory (with the allowed extensions), and selecting one by doing a simple MOD of the day. So, each day it goes to the next one in the directory. Thus, its not completely random. But meh, it does the job!

All you need to do is set the Directory you wish it to index, place some files with .css extension in there and it will return them. The extensions array holds the different extensions you wish it to index.

Feel free to modify it to whatever your requirements are :P I'm sure a script like this has many uses.

<?PHP

/*
 *
 * PHP CSS Randomizer
 *
 * Copyright Stephen Rees 2006
 * http://valorin.net/
 *
 */

$dir = "./";

unset($extensions);
$extensions[] = "css";

$openDir = @opendir($dir); 
if (!$openDir) {
   echo "Unable to access {$openDir}";
}else{
    unset($rFiles);
    while ($file = readdir($openDir)) { 
        foreach ($extensions as $extension){
            if (preg_match("%.{$extension}%", $file) && $file != "." && $file != ".."){
                $rFiles[] = $file;
            }
        }
    }
}

sort($rFiles);
$day = date("z");

header('content-type:text/css');
include($dir.$rFiles[($day%count($rFiles))]);

?>
All the code provided here is avaliable for you to do whatever you like with, so feel free to implement these as part of your own scripts, just don't take my code as-is and change my name to yours.