ASP Banner Ad System

By Michael Ferrini


To the Reader from Joe: This is a user-submitted tutorial by the author above. I have read the tutorial and set the format to fit HTML Goodies, but for the most part have not changed the language. I chose this tutorial because many readers have been asking for more ASP tutorials. This is a great one.

Sorry I cannot show you the event here. The HTML Goodies servers do not offer ASP. I will tell you though that if you run IE5.0 or better, open the contents of the zip file into a directory and it runs just fine.

If you haven't already, you may want to read my introductory ASP tutorial before this one. If not, then enjoy.


    There may be a point in your web design career, where your site becomes real popular. That is when companies become interested in advertising on your site. A Banner Ad system can be built to control all those advertisements that you are so willing to display, for a price. Active Server Pages makes it very easy to create a banner ad system. So easy, that the Microsoft ASP developers created an "AdRotator" component for the occasion. Before you begin reading this article, make sure you download the support material below.

Banner System Zip
All the files you need are right here

The files included are

ad.txt
banner.asp
3 banner images
clicks.asp
example.asp
redirect.asp

ad.txt

    In order for the AdRotator component to work, you must configure a text file. This text file contains all the banner ad properties. However, The text file must follow a certain format. The first four lines are as follows...

REDIRECT redirect.asp
WIDTH 400
HEIGHT 50
*
REDIRECT

    When a banner is clicked, the "AdRotator" component goes to a preliminary page. This page is called a redirect page. The redirect page handles any extra programming events before directing a user to the banners destination. In this example banner system, I called the preliminary file "redirect.asp".

WIDTH

    This sets the width of the banner ad image. The value must be in pixels.

HEIGHT

    This sets the height of the banner ad image. The value must be in pixels.

*

    The asterisk tells the "AdRotator" component that it is about to acquire banner ad information. The asterisk is required.


    Once you define the general properties above the asterisk, then comes the list of banners to display. In the ad.txt file, there are three banners defined below the asterisk.

banner1.jpg
http://www.htmlgoodies.com
HTMLGoodies.com
20
banner2.jpg
http://www.yahoo.com
Yahoo.com
30
banner3.jpg
http://www.dogpile.com
Dogpile.com
30

    Each banner requires four lines of properties, which follow the format below...

Image filename
Web Address 
Description
Banner Weight
Image File

    The image filename can be a fully qualified web address or relative name that points to the image. If the image is in a different folder, then you also include the folder name as well.(http://www.test.com/banner1.jpg, banner1.jpg, or foldername/banner.jpg)>

Web Address

    The web address can be a page on your site or a fully qualified web address that leads to another site.

Description

    The description will be displayed as a tool tip. When you rest your mouse over the banner, the description pops up.

Banner Weight

    The banner weight determines how much a banner is displayed. The "AdRotator" component adds all of the banner weights and determines the probability or percent chance of a particular banner being displayed. A banner with a higher weight has better a better probability.

NOTE: You can disable a banners property by substituting with a dash.

banner3.jpg
-
Dogpile.com
30

    The example entry above would create a banner ad that does not have a web address.

Banner.asp

    This file uses the "AdRotator" component and analyzes the contents of the ad.txt file. Below is the code.

sub banner(strTarget)
   dim bannerad, html
   set bannerad = server.CreateObject("MSWC.adrotator")
   bannerad.TargetFrame = strTarget
   html = bannerad.GetAdvertisement("ad.txt")
   Response.Write html
end sub

    The first thing to note is that the ASP was written with VBScript. The second thing to note is that the code is written inside a sub procedure called banner(strTarget). For those of you who do not know, a sub procedure allows you to group together a bunch of code that can be reused over and over. Like a function, it takes an argument, such as a word or variable. In the code above the argument is strTarget. Unlike a function, a sub-procedure does not return any values, it just executes the code inside line by line.

    Inside the sub I declare two variables...

dim bannerad, html

    Next I store the "AdRotator" component inside the "bannerad" variable. When storing a component inside a variable you use the set keyword. Since we are programming server-side with ASP, we use server.CreateObject to summon the component. "MSWC.adrotator" is the component key or name value.

set bannerad = server.CreateObject("MSWC.adrotator")

    Next I use a property of the "AdRotator" called "TargetFrame". This property is equivalent to...

<a href="http://www.someaddress.com" target="target-value">

    The target-value can be one of the values below...

_blank Opens the destination link in a new browser window.
_top Opens the destination link on top of the web page.
_parent Opens the destination link within a parent frame.
_new Opens the destination link in a new browser window.

    Next, I use an "AdRotator" method or function called "GetAdvertisement" and stored its results in the "html" variable. Notice that the "GetAdvertisement" takes the name of the ad banner text file as an argument.

html = bannerad.GetAdvertisement("ad.txt")

    Finally, I want to print the contents of the "html" variable. This prints the code that displays the banner images.

Response.Write html

Redirect.asp

    This is the file that is processed before someone is redirected to the banners web address. Inside this file, we can capture information like how many times a particular banner is clicked and so on. To start things off, I defined a variable called "strUrl".

Dim strUrl

    Next I store a querystring value inside this new variable.

strUrl = Request.Querystring("url")

    A querystring is nothing more than a bunch of name/value pairs attached to a web address. When a user clicks on a banner, the "AdRotator" component attaches a querystring to the redirect file. So if we were to click banner1.jpg, defined in ad.txt, we would end up with a redirect web address that looks like so...

Redirect.asp?url=http://www.htmlgoodies.com&image=banner1.jpg

    In essence assigning "Request.Querystring("url") to "strUrl", is the same as assigning http://www.htmlgoodies.com to it.

    Finally, I check to see which banner was clicked. I accomplish this with the VBSCript inStr( ) function.

if instr(strUrl, "htmlgoodies") then
   Application.Lock
   application("htmlgoodies") = application("htmlgoodies") + 1
   Application.UnLock
   Response.Clear
   Response.Redirect strUrl
end if

    The inStr( ) function returns the number position of a sub-word (sub-string) within another word (string). The format is as follows

InStr(main word, sub-word)

    If the sub-word exist within the main word, then the function will equal a number greater-than zero or true. If the sub-word does not exist, then the function will equal zero or false. In the example above, I check to see if "htmlgoodies" exist within http://www.htmlgoodies.com. Since the answer is true, then the code inside the if... then... statement will execute.

    Inside the if... then... I use an application variable. An application variable is native to ASP. Application variables store information as long as a web application exist, a web application ceases to exist when say someone shuts off the web hosting server. The beauty of an application variable is that you can define it on one web page and use it in other web pages within your web application. The downfall is that the users computer must have cookies enabled.

    Anyways, the code adds one to the application variable, every time a banner is clicked. After one is added, the code redirects to the banners web page. So if banner1 was clicked then you shall be redirected to "http://www.htmlgoodies.com".

Response.Redirect strUrl

Example.asp

    This is an example page that uses the banner ad system. When you refresh the page, you should most likely see a different banner. Whenever you want to insert the banner ad on a page, you can use the SSI directive below...

<!-- #include file="banner.asp" -->

    Once you include the file above, then you can call the sub-procedure inside the banner.asp file like so...

Call banner("_blank")

    Notice that I supply one of the values for the "TargetFrame" as an argument. So if the banner is clicked, then the web page should open up in a separate browser window.

Clicks.asp

    This is a very simple page that displays the number of clicks per banner ad. To display the number of times a banner was clicked, you just print the contents of the application variables that were created inside "redirect.asp". Pretty nifty.

<% =application("htmlgoodies") %>

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •