三、显示广告
这部分程序主要有如下四个目的:第一,参考各个广告的显示等级,随机选择本次显示的广告;第二,更新数据库中该广告的显示次数;第三,输出标题广告的html代码;第四,在数据库历史表中保存显示和点击历史纪录。
实现上述功能的脚本主要有两个:
showbanner.asp:确定本次调用要显示的广告,更新显示次数记录,生成标题广告的html代码。
redirect.asp:重定向页面。showbanner.asp生成的广告html代码将调用该脚本,由该脚本记录点击历史数据、重定向到广告客户指定的页面。
本文下载包中的showbanner.asp同时也是一个广告显示示例页面,每次刷新该页面可以显示出不同的广告。下面我们按照showbanner.asp的执行过程,介绍其中的关键步骤。
㈠ 计算显示等级总和
本系统采用的广告选择算法是:首先计算出当前所有可用广告的显示等级总和,然后根据这个和值生成一个随机数,再根据这个随机数来确定本次显示的广告。下面的代码用于计算可用广告的显示等级总和:
ntotalweight = 0
strsql = "select sum( weight ) as sumweight from " + _
"advertisement where status=1"
rs.open strsql, cn
if not rs.eof and not rs.bof then
ntotalweight = rs.fields( "sumweight" )
ntotalweight有可能为空值
if isnull( ntotalweight ) then
ntotalweight = 1
end if
end if
在计算出ntotalweight之后,我们可以根据该值生成一个随机数,如下所示:
randomize
nrandomnumber = int( rnd * ntotalweight ) + 1
程序将从数据库读取可用广告记录(参见下面的代码),累计已读取记录的weight总和,当这个新的总和超过这里生成的随机数时,就将当前记录作为本次显示的广告。
㈡ 分析可用广告记录
接下来的任务是执行一个查询,提取所有可用广告记录。如前所述,在遍历这些可用广告记录时,记录已读取记录的weight值总和nweightcount ,当nweightcount 等于或大于前面生成的随机数nrandomnumber时,程序就认为找到了本次要显示的广告。具体如下:
strsql = "select * from advertisement where status=1"
rs.close
rs.open strsql, cn
bdone = false
已读取记录的weight值总和
nweightcount = 0
清除变量
strimageurl = ""
stralttext = ""
strlink = ""
nimagewidth = 0
nimageheight = 0
nweight = 0
nadid = 0
nadvertiserid = 0
nviewlimit = 0
nimpressions = 0
while not rs.eof and not rs.bof and not bdone
将数据库值赋给变量
这种算法不利于效率,但简化了控制结构
strimageurl = rs.fields( "imageurl" )
stralttext = rs.fields( "alttext" )
strlink = rs.fields( "link" )
nimagewidth = rs.fields( "imagewidth" )
nimageheight = rs.fields( "imageheight" )
nweight = rs.fields( "weight" )
nadid = rs.fields( "adid" )
nadvertiserid = rs.fields( "advertiserid" )
nviewlimit = rs.fields( "viewlimit" )
nimpressions = rs.fields( "impressions" )
nweightcount = nweightcount + nweight
rs.movenext
nweightcount是否等于或大于随机变量值
if nweightcount >= nrandomnumber or rs.eof then
bdone= true
end if
wend
㈢ 更新当前广告的显示次数
在确定本次要显示的广告之后,程序就可以更新该广告记录的impressions字段。如果新的impressions值超过了显示次数限制(nviewlimit),还必需设置status字段为0(即本广告不可再显示)。实现代码如下:
nstatus = 1
nimpressions = nimpressions + 1
if nimpressions >= nviewlimit then
nstatus = 0
end if
strsql = "update advertisement set status=" + _
cstr( nstatus ) + ", impressions=" + _
cstr( nimpressions ) + " where adid=" + _
cstr( nadid )
rs.close
rs.open strsql, cn
㈣ 生成html代码
在完成上述准备工作之后,接下来就可以输出显示广告的html代码。所输出的html代码可以分成如下几个部分:
重定向页面 –> 目标url –> 其他参数 –> 图片标记
为什么不是直接链接到广告客户指定的url呢?这是因此,虽然这种方法更简单,但我们希望能够记录广告的点击数量,在重定向页面中我们就可以更新该广告的clickthroughs字段值。
在显示广告的html代码中,重定向页面本文假定为redirect.asp,目标url来自数据库,其他参数主要是广告编号、广告客户编号等,redirect.asp利用这些参数记录点击历史纪录。具体实现如下:
strhtmlcode = _
"<a href=redirect.asp?" + _
"link=" + strlink + _
"&advertisementid=" + cstr( nadid ) + _
"&advertiserid=" + cstr( nadvertiserid ) + chr( 34 ) + _
" target=newframe><" + chr( 13 ) + chr( 10 ) + _
"></a>" + chr( 13 ) + chr( 10 )
这里的strhtmlcode即为显示广告的html代码。
㈤ 记录其他信息
为了给广告客户提供更多的信息,同时也为了便于进一步分析,除了记录显示次数之外,程序还在数据库表中记录其他一些信息(浏览本次广告时,浏览者所在的ip地址以及浏览时间、广告编号、广告客户编号),如下所示:
strsql = "insert into bannerhistory " + _
"(advertiserid, adid, ipaddress, type) values (" + _
cstr( nadvertiserid ) + ", " + _
cstr( nadid ) + ", " + _
request.servervariables( "remote_host" ) + _
", 1)"
rs.open strsql, cn
浏览的日期时间值在access数据库内设置,即设置字段默认值为now()。
㈥ 重定向页面
本文提供一个简单的重定向页面redirect.asp,其功能是记录广告点击数量,然后生成历史表bannerhistory中的记录,最后重定向到目标url,如下所示:
连接数据库,创建记录集对象rs,略…
增加广告的点击计数,如点击计数超过限制值,
则设置status为0
strsql = "update advertisement " + _
"set clickthroughs=clickthroughs+1, " + _
"status=iif(clickthroughs>=clickslimit,0,1) " + _
" where adid=" + request.querystring( "advertisementid" )
rs.open strsql, cn
生成广告点击历史纪录
strsql = "insert into bannerhistory " + _
"(advertiserid, adid, ipaddress, type) values (" + _
request.querystring( "advertiserid" ) + ", " + _
request.querystring( "advertisementid" ) + ", " + _
request.servervariables( "remote_host" ) + ", 2)"
rs.open strsql, cn
重定向到广告客户指定的页面
response.redirect( request.querystring( "link" ) )
next steps
