When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Sunday, June 13, 1999

Creating a Banner Rotation System

So, you want to create a banner rotation system for your website, eh? Well, you have a number of options on how to do it. If you do not need to keep track of how many times the banners are displayed, and how often they are clicked, then the simplest approach is to use Microsoft's Ad Rotator, a component that comes free with ASP! There is a great article already on 4Guys that explains how to use the Ad Rotator. So, if this solution fits for you, by all means, read the Ad Rotator article!

- continued -

If you want to keep track of the number of times the banners are displayed, and how often they are clicked, you will need to take a somewhat different approach. There are really two ways to do this: use text files to keep track of your impressions and click throughs, or use a database. For this article, I am going to discuss using text files. There is an article on 4Guys which discusses displaying advertisements using a database. That article, by Bart Silverstein, is called Automatically Configured Advertisement Display.

To keep track of all of our banners, we will create a text file named BANNERS.TXT. This text file will have the following format:


Each record will be separated by an asterik (*) on its own line. So, if we had three banners, BANNERS.TXT just might look like this:

This is the banner for 4GuysFromRolla.com
This is the banner for ASPMessageBoard.com
This is a banner advertising Yahoo!

Notice that between each record there is an asterik (*). This is important. Also note that each banner has a unique BannerID (the first number in each record). The second number is the AdvertiserID. When an advertiser goes to view his banner stats, he will see a report for all of the banners that have his advertiser ID. The third number is the weight. This is how often each banner will appear. An easy way to calculate the percentage of times a given banner will appear is to divide the weight by the sum of the banner weights. So, the first banner has a weight of 20, and will show 20 / (20 + 30 + 50) = 0.20, or 20% of the time. (If you make sure that all of the weights add up to 100, then the weight for a given banner is also the exact percentage of times it will show.)

There are three files you'll need to add to your website to get this working. One is banner.asp. You'll need to include this file into all the pages that you want to display a banner on. (If you're unfamiliar with how to include files in ASP, be sure to check out this article.) In the file, to display a banner at a given location, just insert this single ASP line of code:

<% ShowBanner %>

That's all there is to it! You'll have to make some minor modifications to banner.asp. Namely, you'll have to change some of the constants there to make it a better fit on your system. Also, you'll need to add the subdirectories to whatever directory you put your banner scripts in. These two directories must be named clicks and clickthrus. They will keep track of each banners impressions and click throughs. (Banner.asp, as well as the other two ASP files, are available for download at the bottom of this article.)

You may want to read a couple of articles before delving into banner.asp. I highly recommend you have a firm grasp on the FileSystemObject and on using split and join. If you don't know what FSO or split and join are, then please read these two articles first!! Thanks!

The next file you'll need is named adredir.asp, and it is the file that is called when someone clicks on a banner. This file is responsible for two tasks: first, it needs to update the click through stats, and second it needs to send the user to the appropriate site.

The last file is report.asp. This file will display a report for a given AdvertiserID. I recommend that you have a unique ASP page for each advertiser's report. So, let's say we had Yahoo! as an advertiser. I might make their stats page available at http://www.myserver.com/reports/yahooreport.asp. In YahooReport.asp, I would need to first include report.asp, and then, wherever I wanted to report displayed, would need to add the line:

<% PrintReport 5 %>

(assuming Yahoo's advertiser ID was 5). That's all there is to it! I hope you find this helpful! Remember, some of the topics covered in the included .ASP files are rather advanced, so you may need to read some primer articles first!

After looking through the banner.asp file, you may wonder why I have a CategoryID. Well, this isn't used at all in the implementation here, but one could easily implement a system using the CategoryID where certain banners only appear in certain sections of the site. For example, if your site dealt with ASP information and Humor, and you wanted Humor banners to appear in the humor section and ASP banners to appear in the ASP section, you could do so by specifying a category when calling ShowBanner. Right now, though, that's not supported, but I encourage you to add this functionality! :) Happy Programming!


  • Banner.asp
  • AdRedir.asp
  • Report.asp

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article