当前位置导航:炫浪网>>网络学院>>网页制作>>ASP教程

动态广告管理程序制作例子

  By Wayne Berry
This Issue
Many sites that are content specific depend on banner advertisement for revenue. Such is the case for 15
Seconds. A banner is displayed at the top of the page for every page viewed. Clients usually buy a set
number of banner impressions and these impressions are rotated amongst all the clients over a period of
time. In order to do this there must be a dynamic quality to the banners. In other words, the banner to
display is not determined until that page is requested.
With an Active Server page this is an easy task. Active Server Pages are dynamic and there are many
components that handle the task of banner rotation. A simple one comes free with the Internet Information
Server, and more complicated rotation schedules can be done with Site Server and other third party
components. This article is not about how to implement these components within your Active Server pages,
since this is a fairly easy task.

This article will describe how to implement a banner rotation scenario where the pages served are static,
i.e. .htm, and do not have the flexibility to call components. The task is to dynamically serve banners
and statically serve pages. This is the opposite of the scenario for banner rotation components, which
statically serve banners and dynamically serve pages.

One Trick
There is only one trick here and once you know it the rest is pretty straightforward. Internet Explorer
and Netscape allow you to refer to an Active Server Page within an image tag. Example 1 demonstrates the
HTML to be inserted in the static page.
Example 1 : HTML Banner Reference



< IMG SRC="http://www.myserver.com/ServeBanner.asp">


Once you have referred to a dynamic Active Server page with the static page, you need to build an Active
Server page to return an image, in this case a banner. Example 2 shows the code to use for servebanner.asp
which is called in Example 1.

Example 2 : ServeBanner.asp Source



<%
Response.Redirect("http://www.myserver.com/banner.gif?http://www.xvna.com")
%>


These two pages combined will allow you to display to the user a banner as if you referred directly to the
banner image instead of an Active Server page. In order to do banner rotation all you have to do is expand
Example 2 so different images come up every time ServeBanner.asp is called.

Adding the Element of Rotation.
With this technique the dynamic aspect of the banner rotation is controlled within the Active Server page
that the IMG tag is referring to. In order to randomly rotate the banners you will need to change
ServerBanner.asp to choose a different banner each time that it is called. Example 3 shows how to do this.
Example 3 : Rotating Banners



<%

Dim Array(2)

' Initialize the VBScript Random
' Number Generator
Randomize

Array(1)="http://www.myserver.com/banner1.gif?http://www.xvna.com"
Array(2)="http://www.myserver.com/banner2.gif?http://www.xvna.com"

upperbound=UBound(Array)
lowerbound=1
lRandom = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

Response.Redirect(Array(lRandom))

%>


Notice that you will have to initialize the array for as many banners as are available and you will have
to assign a banner to each element of the array.

An Extra Benefit
An obvious benefit to this type of banner rotation is that fact that you don't have to serve Active Server
pages in order to have rotating banners. If the only dynamic aspect of your Active Server page is to
rotate banners you will want to change it to use this banner rotation technique, since it will reduce the
stress on your server.
It might not be so obvious that the static pages no longer need to reside on your machine to rotate
banners. For example, you could give the HTML in Example 1 to a site that you were advertising on, and
when they serve there pages with your HTML your server will rotate the banners for their site. This allows
you control of what banners are presented to their readers. The site that was serving the static pages
could even be a different operating system, such as UNIX.

Clicking on the Banner
So far we have demonstrated how to rotate banners, but not how to activate those banners so that you can
click on the banner to go to another site. With one banner in the rotation this is a straightforward
process, you just point the anchor tag to the site which the banner represents. However when you have
multiple banners in the rotation it becomes more difficult. The problem is that a random banner is served
every time you request a page, however the anchor tag on that page does not change, since the page is
static.
To solve this problem the anchor tag needs to reference an Active Server page that can determine what
banner was being displayed so that it can redirect the browser to that page. Example 4 shows what the HTML
will look like to activate the banners in this banner rotation technique.

Example 4 : Activating the Banners



< A HREF=" http://www.myserver.com/BannerClick.asp">
< IMG SRC="http://www.myserver.com/ServeBanner.asp">
< /A>


BannerClick.asp now has the responsibility of redirecting to the correct location. However, currently
there is no way for BannerClick.asp to know which banner is being displayed to the user. In order to
determine this, ServeBanner.asp must tell the browser which banner it is going to display so that if the
user clicks through BannerClick.asp can ask the browser what banner was being shown. One way to do this is
to use cookies?. Example 5 shows how to modify ServeBanner.asp so that it sets a cookie every time a
banner is served.

Example 5 : Rotating Banners with Cookies



<%

Dim Array(2)

' Initialize the VBScript Random
' Number Generator
Randomize

Array(1)="http://www.myserver.com/banner1.gif?http://www.xvna.com"
Array(2)="http://www.myserver.com/banner2.gif?http://www.xvna.com"

upperbound=UBound(Array)
lowerbound=1
lRandom = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

Response.Cookies("BannerId")=lRandom
Response.Redirect(Array(lRandom))

%>


Once ServeBanner.asp is coded so that it issues a cookie for each request, and that cookie is the banner
identifier in the array, we can program ClickBanner.asp so that it reads the cookie and redirects to the
correct location. Example 6 shows how to implement ClickBanner.asp.

Example 6 : ClickBanner.asp



<%

Dim Array(2)

lBannerId  = Request.Cookies("BannerId")

Array(1)="http://www.client1.com"
Array(2)= "http://www.client2.com"

Response.Redirect(Array(lBannerId))

%>


Notice that the array is used differently in ServeBanner.asp then it is used in ClickBanner.asp. In
ServeBanner.asp the array referenced banner graphics, in ClickBanner.asp the array references the location
to go to when the banners is clicked. Since the index in the array is used in both locations the array
must be populated so that for every index the banner image matches the location.

Handling Netscape
The Netscape browser has a bug in it that needs to be avoided when rotating banners using this technique.
If you do not specify the size of an image in HTML Netscape queries the image itself to determine the
size. This is a feature that both Netscape and Internet Explorer have. However, Netscape queries the first
response to the image call for the size. Using this technique the first response is a set of headers that
are suppose to redirect the browser to the real graphic. When Netscape queries the headers for the image
size it fails since the response its not an image. In order to avoid this problem you will need to specify
the banner image size in the HTML. Example 7 shows Example 4 rewritten to specify the image size.
Example 7 : Setting the Image Size



< A HREF=" http://www.myserver.com/BannerClick.asp">
< IMG WIDTH=468 HEIGHT=60 SRC="http://www.myserver.com/ServeBanner.asp">
< /A>


Because of this bug there is no way to serve banners that are of varying sizes, since the size is
specified in a static page which does not change with the banner that is served.  
Beyond
The next step in creating your own banner rotation application is to put the banner arrays in a database
and to query the database every time a page is requested. This would allow you the flexibility to change
the banners using another set of Active Server pages which administers the database.
You could also implement a better rotation schema that would have a daily cap on the number of impressions
served and a maximum number of impressions to serve. You might also want to implement a default banner, so
that when the maximums are met there is something to rotate.

Another interesting addition would be the logging of the number of banners served and the number of click
throughs per banner. Then creating a set of Active Server pages to display the statistics in a variety of
forms.

However, before you go off and build yourself a database, and implement complicated banner rotation
algorithms you might want to consider buying a product that already does what you want. Banager
(http://www.banager.com) is an Active Server page application that is implemented using the techniques
discussed in this article. However, Banager has statistical pages, database logging and banner rotation
algorithms built in. In my opinion it is well worth the cost.

 
相关内容
赞助商链接