Simple, Fast and Light-Weight Banner Advertisement Rotator using Server Side Includes (SSI)

Here’s a code snippet which will let you rotate multiple banner advertisements for all the ad spaces you have on your HTML pages. I wrote this because I didn’t want to use an ad server and wanted a light weight yet elegant solution. What the below code does is, it creates a SSI variable ‘slot’ which can have possible values of 1-12. Each value corresponds to a 5 second slot. Source this file at the start of your HTML file and then use the slot variable anywhere in your HTML code to rotate data according to time based slots.

<!--#config timefmt="%S" -->
<!--#set var="rand" value="$DATE_LOCAL" -->
<!--#if expr="$rand = /[0][0-4]/" -->
<!--#set var="slot" value="1"-->
<!--#elif expr="$rand = /[0][5-9]/" -->
<!--#set var="slot" value="2"-->
<!--#elif expr="$rand = /[1][0-4]/" -->
<!--#set var="slot" value="3"-->
<!--#elif expr="$rand = /[1][5-9]/" -->
<!--#set var="slot" value="4"-->
<!--#elif expr="$rand = /[2][0-4]/" -->
<!--#set var="slot" value="5"-->
<!--#elif expr="$rand = /[2][5-9]/" -->
<!--#set var="slot" value="6"-->
<!--#elif expr="$rand = /[3][0-4]/" -->
<!--#set var="slot" value="7"-->
<!--#elif expr="$rand = /[3][5-9]/" -->
<!--#set var="slot" value="8"-->
<!--#elif expr="$rand = /[4][0-4]/" -->
<!--#set var="slot" value="9"-->
<!--#elif expr="$rand = /[4][5-9]/" -->
<!--#set var="slot" value="10"-->
<!--#elif expr="$rand = /[5][0-4]/" -->
<!--#set var="slot" value="11"-->
<!--#else -->
<!--#set var="slot" value="12"-->
<!--#endif -->

What the above code snippet does is, whenever a user requests a page, it looks at the seconds (hand) of the server’s internal clock and uses some simple regex to figure out which slot is active currently.

Save the above code in a file (lets say banner-advertisement-rotator.html) and copy it to the root directory of your website.

Next, include this file in all your HTML files. The next code snippet shows you how to.

<!--#include virtual="/banner-rotator-variables.html"-->

Now to use the slot variable, here is some sample markup that you can use. This markup can be replicated in multiple places in the HTML page to rotate different banner advertisements.

<!--#if expr="$slot = 1"-->
  <!--#include virtual="/bannerads/slot1.html"-->
<!--#elif expr="$slot = 2"-->
  <!--#include virtual="/bannerads/slot2.html"-->
<!--#elif expr="$slot = 3"-->
  <!--#include virtual="/bannerads/slot3.html"-->
<!--#elif expr="$slot = 4"-->
  <!--#include virtual="/bannerads/slot4.html"-->
<!--#elif expr="$slot = 5"-->
  <!--#include virtual="/bannerads/slot5.html"-->
<!--#elif expr="$slot = 6"-->
  <!--#include virtual="/bannerads/slot6.html"-->
<!--#elif expr="$slot = 7"-->
  <!--#include virtual="/bannerads/slot7.html"-->
<!--#elif expr="$slot = 8"-->
  <!--#include virtual="/bannerads/slot8.html"-->
<!--#elif expr="$slot = 9"-->
  <!--#include virtual="/bannerads/slot9.html"-->
<!--#elif expr="$slot = 10"-->
  <!--#include virtual="/bannerads/slot10.html"-->
<!--#elif expr="$slot = 11"-->
  <!--#include virtual="/bannerads/slot11.html"-->
<!--#else -->
  <!--#include virtual="/bannerads/slot12.html"-->
<!--#endif -->
Make sure you have Server Side Includes (SSI) enabled for your server. The typical way of enabling SSI is by adding AddHandler server-parsed .html to your .htaccess file.

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

Leave a Reply