Random Band Banner
in Popmundo

Added on October 28th, 2009

A few people have asked me how the BEC. band banner works. What’s so special about it? Well every time you view the band page it shows a different banner. So how does that work? Quite simple really.

With the help of PHP and mod_rewrite (Apache module) you can achieve the same effect for your band. Should you want to use multiple banners for it that is. You can also use this technique for anything where you’d like to have a random rotating image and can only link to a static image link.

So let’s get started on it then!

First you’ll need your images and you’ll want to put them inside a subfolder called “images” (you can use a different folder name if you’d like, but don’t forget to update the banner.php file then).

Next you’ll need a file called banner.php, and here’s the source code for it:

<?php

$counter = 0;
$files = array();
$images = 'images';

$handle = opendir($images);
while (false !== ($file = readdir($handle)))
{
  if (eregi(".jpg$|.jpeg$", $file))
  {
    $files[] = $file;
    $counter++;
  }
}

$random_temp = mt_rand(0, $counter - 1);
$random_img = $images . '/' . $files[$random_temp];
$random_size = filesize($images . '/' . $files[$random_temp]);

# Send headers
header("HTTP/1.1 200 OK");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: image/jpeg");
header("Content-Length: $random_size");

# Output the image
readfile($random_img);

?>

What happens here basically is that the images folder is read for any jpg files, those are then put in an array, and a random image is picked out from the array and then shown to the user.

So if you were to upload the image folder and banner.php to your server now, you could already see it in action by opening the banner.php file in your browser.

BEC. banner.php in action.

Now the last thing we need to do is to create an .htaccess file which will use Apache’s mod_rewrite module to allow us to use a static image URL to point to the banner.php file.

Create a text file called htaccess.txt and copy this inside it:

RewriteEngine on
RewriteRule ^banner.jpg$  banner.php [L,NC,QSA]

And that’s basically it. Now you just need to upload everything to your webserver, and don’t forget to rename htaccess.txt to .htaccess (yes, it’s just a dot followed by htaccess) and you should have it all working.

BEC. banner.jpg in action.

If you have any questions, comments or suggestions, don’t hesitate to post them!

Go back


2 replies to “Random Band Banner
in Popmundo”


Hi, sorry for this offtopic question but i dont find the RSS Feed Link to add this Blog to my Feedreader. Could you please give me the URL? Thanks a lot.

Greetings from Switzerland


Bottom of the right navigation. Or: http://www.rihardsonline.com/feed/