Someone named "Jon" (no email) asked me for my random banner code.  Since I know about 8 Jons, I’ll just post it here and hope he reads my blog regularly.

The image at the top of my blog changes each time you reload (or it’s supposed to.  It’s random, so you COULD get the same one twice).  The code that simply grabs all the filenames in a specified directory, strips off all the peripheral info, and picks on at random.  Then you drop that into an img tag and you have a random image.

So here’s the code:

$banner = array();
if ($handle = opendir($_SERVER['DOCUMENT_ROOT'] . '/phlog/graphics/headers/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && (!strstr($file,'index')) && (!strstr($file,'htaccess')))
            // echo "<a href="$file">$file</A><br>n";
            $banner[] = $file;


$rand_keys = array_rand($banner, 2);

print $banner[$rand_keys[0]];


3 thoughts on “Random banner code

  1. I didn’t even notice you had “random banners.”
    Maybe you should overlay “Push F5 cuz I’m random” over the banner graphics.

    -cvh (if you know multiple, i’m the cvh that has enriched your life the most)

Leave a Reply

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