Rotating Advertisements

Advertising is big business on the Web. This lesson describes how to use the Ad Rotator component, installed with IIS, to rotate advertisements on your Web pages. The Ad Rotator component selects an advertisement for your Web page each time the user loads or refreshes the Web page. Furthermore, if you want to change an advertisement, you only need to change it in the redirection and rotation schedule files, instead of changing all the ASP files that contain the advertisement. This saves development time if the advertisement appears on numerous pages within your Web site.

Two files are required to set up the Ad Rotator component: a redirection file, which contains URL links to ads, and a rotation schedule file, which contains display data. By setting up these two files, the Ad Rotator component can be called by any ASP page on your Web site.

In this lesson you perform the following tasks:

  • Example 1: Create an Ad Rotator rotation schedule file that creates ad-image links on any page that calls this file.

  • Example 2: Create an Ad Rotator redirection file that specifies global ad-display data and information specific to each advertisement.

  • Example 3: Create an include file to hold your Ad Rotator calling code.

  • Example 4: Test the Ad Rotator by creating an ASP page that calls the Ad Rotator component to display and rotate ads. This example requires that examples 1, 2, and 3 are completed first.

Example 1: Create an Ad Rotator Rotation Schedule File

A rotation schedule file is used to catalog information about the ads you want to display, including redirection information after the advertisement is clicked on, the size of the displayed advertisement, the image to display, a comment for the advertisement, and a number that indicates how often a particular advertisement is chosen. When methods of the Ad Rotator component are called in an ASP page, the component uses this file to select an advertisement to display.

The rotation schedule file is divided into two sections that are separated by an asterisk (*). The first section provides information common to all the ads, and the second section lists specific data for each ad. To test the rotation schedule file, you will use some images from for your ad images. The following list outlines the structure of the rotation schedule file:

Section 1

  • Redirection: In URL form, the path and name of the ASP file that can be executed before showing an advertisement. This file can be used to record information about the user who clicks on your ad. You can record information such as the client's IP address, what page the client saw the advertisement on, how often an advertisement was clicked on, and so forth. This ASP file can also handle the case where there is no URL associated with any advertisement in Section 2. When charging advertisers for each hit on their advertisement, it is good practice to prove to them that all the hits aren't resulting from the same user repeatedly clicking Refresh.

  • Width: The width of each ad image, in pixels. The default is 440.

  • Height: The height of each ad image, in pixels. The default is 60.

  • Border: The border thickness around each ad image. The default is 1.

  • Asterisk (*): Separates the first section from the second section. This character must be on a line by itself.

Section 2

You need to complete the following for each advertisement:

  • Image URL: The virtual path and filename of the image file for the advertisement.

  • Advertiser's Home URL: The URL to jump to when this link is selected. If there is no link, use a hyphen (-).

  • Text: The text to display if the browser does not support graphics.

  • Impressions: An integer indicating the relative weight to give to this ad when the Ad Rotator component selects an advertisement. For example, if you list two advertisements, an ad given an impression of 3 has a 30% probability of being selected while an ad given an impression of 7 has a 70% probability of being selected. In this example, the Ad Rotator component selects the Microsoft Windows advertisement two times out of five and the Microsoft Office advertisement is selected three times out of five.

Copy and paste the following code in your text editor, and save the file as MyAdRot.txt in the x:\Inetpub\Wwwroot\Tutorial directory.

  REDIRECT AdRotRedirect.asp  
  WIDTH 250  
  HEIGHT 60  
  BORDER 0  
  Microsoft Windows  
  Office 2000  

Example 2: Create an Ad Rotator Redirection File

When a user clicks on an advertisement, an Ad Rotator redirection file written in ASP can capture some information before showing the advertisement and write that information to a file.

For this to work, the x:\InetPub\Wwwroot\Tutorial folder must give Read/Write access to the IUSR_ ComputerName and IWAM_ ComputerName accounts. Alternatively, you can write this information to a Microsoft Access database.


In the code below, Server.HTMLEncode is used to encode header fields. In previous lessons, HTMLEncode was used to encode form input only. However, it is possible for a malicious user to manipulate header information, so it is a good security practice to encode all input generated from a request, not just form input.

Copy and paste the following code in your text editor, and save the file as AdRotRedirect.asp in the x:\Inetpub\Wwwroot\Tutorial directory.

<%@ Language= "VBScript" %>  
<title>AdRotRedirect file</title>  

'Create some variables.  
dim strLogFile  

'Get the physical path of this Web directory so that we know the path exists.  
'The ASP Server object has many useful methods.  
strLogFile = Server.MapPath(".") & "\AdRotLog.txt"  

'Set some constants for working with files.  
Const cForAppending = 8  
Const cTristateUseDefault = -2  

'Create a FileSystemObject object,  
' which gives you access to files and folders on the system.  
Set fsoObject = Server.CreateObject("Scripting.FileSystemObject")  

'Open a handle to the file.  
'True means that the file will be created if it doesn't already exist.  
Set tsObject = fsoObject.OpenTextFile(strLogFile, cForAppending, True)  

'Record the data for the user who has just clicked on an advertisement.  
'We have used the Write method of the ASP Request object.  
'The ServerVariables collection of the ASP Request object holds vast  
' amounts of data for each request made to a Web server.  
tsObject.WriteLine "--------------------"  
tsObject.WriteLine Date & ", " & Time  
tsObject.WriteLine Server.HTMLEncode(Request.ServerVariables("LOGON_USER"))  
tsObject.WriteLine Server.HTMLEncode(Request.ServerVariables("REMOTE_ADDR"))  
tsObject.WriteLine Server.HTMLEncode(Request.QueryString("url"))  
tsObject.WriteLine Server.HTMLEncode(Request.ServerVariables("HTTP_REFERER"))  
tsObject.WriteLine Server.HTMLEncode(Request.ServerVariables("HTTP_USER_AGENT"))  

'Redirect to the Advertiser's Web site using the Redirect method  
' of the ASP Response object.  
'When the AdRotator component calls AdRotRedirect.asp, it  
' automatically passes in the advertiser's URL in the QueryString.  
Response.Redirect Server.HTMLEncode(Request.QueryString("url"))  


Example 3: Create an Ad Rotator Include File

Include files are used to store any code that will be used by more than one ASP or HTML file. It makes sense to put your Ad Rotator code into a simple function in an include file. With an Ad Rotator include file, you need to make only one function call from any ASP or HTML file when you want to display an advertisement. Alternatively, you can put the code from the include file in every ASP file where you plan to show an advertisement. However, if you want to change that code, you have to make the change in every ASP file instead of in one include file.

In this example, you create an Ad Rotator include file containing a function named GetAd. This function randomly selects ads to display on your ASP pages.

Copy and paste the following code in your text editor, and save the file as in the x:\Inetpub\Wwwroot\Tutorial directory.

Function GetAd()  

dim objLoad  

'Create an instance of the AdRotator component.  
Set objLoad = Server.CreateObject("MSWC.AdRotator")  

'Set the TargetFrame property, if any. If you have a Web  
' page using frames, this is the frame where the URL opens.  
'If the HTML page does not find the TARGET name,  
' the URL will be opened in a new window.  
objLoad.TargetFrame = "TARGET=new"  

'Set one of the other AdRotator properties.  
objLoad.Border = 1  

'Get a random advertisement from the text file.  
GetAd = objLoad.GetAdvertisement("MyAdRot.txt")  

End Function  

Example 4: Test the Ad Rotator

To test the application you have built on the Ad Rotator component, you need an ASP page that calls the function in the Ad Rotator include file you created.

Copy and paste the following code in your text editor, and save the file as DisplayAds.asp in the x:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing https://localhost/Tutorial/DisplayAds.asp in the address bar.

<%@ Language= "VBScript" %>  

<title>Display an Advertisement</title>  
<font face="MS Gothic">  

<h2>Display an Advertisement</h2>  

<comment>Include the file you created to get an advertisement.</comment>  
<!-- #include File = "" -->  

<comment>Call the Function in the include file.</comment>  


In the browser, you should see the following:

Display an Advertisement

Windows Server 2003 Logo screen

Click the Refresh button in your browser about 20 times to watch the advertisement change. Click the advertisement to see how AdRotRedirect.asp redirects you to the advertiser's Web site. Open AdRotLog.txt to see what was recorded when you clicked on an advertisement.