|
|
|
|
你现在的位置:您现在的位置是: 中国ASP>>ASP教程>>asp类 |
|
| asp类方法初探(广告流量统计系统实践) |
|
作者qq:88583047
写了快一年的asp程序了,多少有些厌烦。由于是学生,最近手头比较紧,接了个广告公司的单,又是asp的项目。不过尝试了下用asp的类来开发。广告公司要求:对广告进行年,月,日,小时的ip记录。要求能查看ip日志,而且要有访问地区百分比图列。并且管理员能对广告进行ip查询。要求能应付日访问 30万以上的流量统计。在开发这个项目的时候,策划2天。找了qq上的ip库。编写类用了一上午,一下午写出了全部显示,统计,管理流程页面。返工一次。因为原来只做了精确到日的快速查询,没有做精确到小时。返工修改30分钟。总共开发时间:3天半。测试一星期,基本没发现bug。 体会:这次开发感觉客户要求返工的次数较少的原因是,初期策划和设计的比较成功。而且将比较难的部分做成功能模块。开发到最后思绪还是比较清楚的。比以前开发同类规模的项目省事很多。。现在我把这个项目的数据结构附出:ip库用access,考虑到客户需要更新ip库,而网上又只能下到access版本的。 其他数据库: sqlserver2000 ACCESS 数据库:IP库
字段名 属性 含义 ==ip表== id 自动编号 唯一的id onip 数字 起始ip offip 数字 结束ip addj 文本 第一地址 addf 文本 结束地址 SQL 数据库: ADCount库 字段名 属性 含义 ==admin表== id int(1,1) 唯一的id usr nvarchar(50) 用户名 pwd nvarchar(50) 密码 lev tinyint 用户等级(0为普通用户,1为管理员) AllCount int 总点击率 info ntext 用户信息 ==ADList表== id int(1,1) 唯一的id ADname nvarchar(50) 广告名 AllCount int 总点击率 link nvarchar(100) 传入地址 UsrID int 用户id(用于标示所属用户) jumpUrl nvarchar(100) info nvarchar(200) 广告信息 ==ADCount表== id int(1,1) 唯一的id ADid int 广告id(用于标示所属广告) MonthNo int 月数 YearNo int 年数 DayNo int 天数 HourNo int 日访问量 DayCount int 小时访问量 Area nvarchar(500) 地区访问量(格式为:“100,234,345,1245”) ==UserCount表== id int(1,1) 唯一的id UserID int 广告客户id MonthNo int 月数 YearNo int 年数 DayNo int 天数 HourNo int 日访问量 DayCount int 小时访问量 Area nvarchar(500) 地区访问量(格式为:"100,234,100,...") ==自动生成表== (自动生成的为日志表:日志表名用"log"+广告id表示) id int(1,1) 唯一的id ip varchar(15) ip地址,字符型 intip int 长整型ip logtime datetime 访问日期 addj varchar(20) 第一物理地址 addf varchar(30) 详细物理地址 area smallint 所在地区(对应为程序中的数组下标) CommonClass类: 公用函数或过程: WrongAlert(strAlert) JumpSuccess(strLink) CheckFormIsEmpty(strSplit) CheckFormIsNumeric(strSplit) ChkStrIn(fString) ChkStrOut(fString) IsWrongStr(fString) CheckFormIsWrongStr(strSplit) CheckFormIsDate(strSplit) CheckFormIsSame(strSplit) HaveRightStr(strSouce,str) IP类: 公用函数或过程: GetStrIP() GetLongIP() GetDetails() 属性: DataPath(ByVal strVar)有默认值 DataTable(ByVal strVar)有默认值 Addr AddrDetails AddrProvince AddrCount类: 公用函数或过程: GetAddrNo(str) 得到地址号 GetCount(str,intNo) 得到字符串的值 GetAddr(intNo) 得到地址,返回字符 EditAddrStr(strSrc,inttag,val) 修改统计结果 AddrCountAdd(strSrc,inttag) 统计结果+1 FirstAdd(inttag) 产生一条新的记录 CreatePic(intArray) 产生或创建图片 DataBind类 公用函数或过程: ShowSelectBox(strValue)将数据绑定到SelectBox中 属性: DataSource(ByVal str)数据源 Table(ByVal str)数据表 CItem(ByVal str)显示值 CValue(ByVal str)真实值 FormName(ByVal str)SelectBox名 第一个类是一个通用的跳转和判断类,其他的项目相关的类,附原代码: Class CommonClass private strAuthor private strVersion Private Sub Class_Initialize() strAuthor = "Tong Luo" strVersion = "1.0" End Sub
Public Property Get Version Version = strVersion End Property
Public Property Get Author Author = strAuthor End Property
''''''''You can pass a string which is show a alert dialog when program make a mistake. Public Sub WrongAlert(strAlert) if not isnull(conn) then conn.close() Response.write ("<script language=''''''''javascript''''''''>alert(''''''''"&strAlert&"'''''''');location=''''''''javascript :history.go(-1)'''''''';</script>") Response.end() End Sub
''''''''You can pass a string which is a address of jump when you get the right result. Public Sub JumpSuccess(strLink) if not isnull(conn) then conn.close() Response.write ("<script language=''''''''javascript''''''''>alert(''''''''操作成功!'''''''');</script>< meta HTTP-EQUIV=refresh Content=''''''''0;url="&strLink&"''''''''>") Response.end() End Sub
''''''''Pass a string separated by "," This Function will judge the form whether is empty. Public Sub CheckFormIsEmpty(strSplit) aForm=split(strSplit,",") for each i in aForm if Request(i)="" then Call WrongAlert("请填写完整!") end if next End Sub
Public Sub CheckFormIsNumeric(strSplit) aForm=split(strSplit,",") for each i in aForm if Request(i)<>"" then if not isnumeric(Request(i)) then Call WrongAlert("数据格式不正确,非数值类型!") end if end if next End Sub
''''''''exchange the special symbol into html public function ChkStrIn(fString) if fString <> "" then fString=replace(fString,"<","<") fString=replace(fString,">",">") fString=replace(fString, "''''''''", "’") fString=replace(fString,"""","”") fString=replace(fString, " "," ") fString=replace(fString, vbCrlf,"<br>") end if ChkStrIn=fString end function
''''''''exchange the html into special symbol function ChkStrOut(fString) if fString<>"" then fString=replace(fString,"<","<") fString=replace(fString,">",">") fString=replace(fString, "’", "''''''''") fString=replace(fString,"”","""") fString=replace(fString," "," ") fString=replace(fString,"<br>",vbCrlf) end if ChkStrOut=fString end function ''''''''check the string whether contain wrong characters public function IsWrongStr(fString) IsWrongStr=false if instr(fString,"<")<>0 then IsWrongStr=true if instr(fString,">")<>0 then IsWrongStr=true if instr(fString,"''''''''")<>0 then IsWrongStr=true if instr(fString,"""")<>0 then IsWrongStr=true end function
public sub CheckFormIsWrongStr(strSplit) aForm=split(strSplit,",") for each i in aForm if request(i)<>"" then if IsWrongStr(request(i)) then Call WrongAlert("数据格式不正确!") end if end if next end sub
Public sub CheckFormIsDate(strSplit) aForm=split(strSplit,",") for each i in aForm if request(i)<>"" then if not isdate(request(i)) then Call WrongAlert("数据格式不正确!") end if end if next end sub
public sub CheckFormIsSame(strSplit) aForm=split(strSplit,",") for each i in aForm if request(i)<>request(aForm(0)) then Call WrongAlert("输入不一致!!") end if next end sub
public function HaveRightStr(strSouce,str) if instr(strSouce,str)=0 then HaveRightStr=false else HaveRightStr=true end if end function
End Class Class IP ''''''''ip操作类 private strDataPath private strAddr private strAddrDetails private strAddrProvince private strDataTable Private Sub Class_Initialize() strDataTable="ip" strDataPath=Server.Mappath("data/ip.mdb") End Sub ''''''''得到字符型ip地址 Public function GetStrIP() userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR") userip2 = Request.ServerVariables("REMOTE_ADDR") if userip = "" then GetStrIP=userip2 else GetStrIP=userip end if end function ''''''''得到长整型ip地址 Public function GetLongIP() inttemp=0 strArrayIP=split(GetStrIP(),".") intMax=Ubound(strArrayIP) for i=0 to intMax inttemp=inttemp+strArrayIP(i)*256^(intMax-i) next GetLongIP=inttemp end function ''''''''设置ip库路径 public property let DataPath(ByVal strVar) strDataPath=strVar end property ''''''''数据表,默认为ip表 public property let DataTable(ByVal strVar) strDataTable=strVar end property public sub GetDetails() connstr="DBQ="+strDataPath+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};" set conn1=server.createobject("ADODB.CONNECTION") conn1.open connstr lngip=GetLongIP() set rs1=conn1.execute("select * from "&strDataTable&" where onip<= "&lngip&" and offip>="&lngip) strAddr=rs1("addj") strAddrDetails=rs1("addf") set rs1=nothing conn1.close() set conn1=nothing end sub public property get Addr Addr=strAddr end property public property get AddrDetails AddrDetails=strAddrDetails end property public property get AddrProvince strtemp=left(strAddr,2) select case strtemp case "黑龙" strtemp="黑龙江" case "内蒙" strtemp="内蒙古" end select AddrProvince=strtemp end property end Class class DataBind private strTable private strCItem private strCValue private strFormName private strconn public property Let DataSource(ByVal str) set strconn=str end property public property Let Table(ByVal str) strTable=str end property public property let CItem(ByVal str) strCItem=str end property public property let CValue(ByVal str) strCValue=str end property public property let FormName(ByVal str) strFormName=str end property public sub ShowSelectBox(strValue) set rs=strconn.execute("select "&strCValue&","&strCItem&" from "&strTable) %> <select name="<%=strFormName%>" id="<%=strFormName%>"> <option>----</option> <% while not rs.eof %> <option value="<%=rs(strCValue)%>" <%if strValue=rs(strCValue) then response.write("selected")%>><%=rs(strCItem)%></option> <% rs.movenext wend %> </select> <% end sub end class %> <% class AddrCount private strProvince Private Sub Class_Initialize() if Setup_Province<>"" then strProvince=Setup_Province End Sub ''''''''获得在字符串里的地址下标号,传入字符串 public function GetAddrNo(str) GetAddrNo=0 if str<>"" then strArray=split(strProvince,",") for i=0 to Ubound(strArray) if str=strArray(i) then GetAddrNo=i next end if end function ''''''''传入以“,”分隔的字符串和整数下标,返回值 public function GetCount(str,intNo) if intNo<>"" then if isnumeric(intNo) then strArray=split(str,",") GetCount=strArray(cint(intNo)) end if end if end function ''''''''通过下标号获得地址,传入整数.此函数可以取地址或数据库中的数字 public function GetAddr(intNo) GetAddr=GetCount(strProvince,intNo) end function ''''''''修改地址字符串,传入要修改的字符串,和修改下标,以及修改的值 public function EditAddrStr(strSrc,inttag,val) arrayTemp=split(strSrc,",") arrayTemp(inttag)=val EditAddrStr=join(arrayTemp,",") end function ''''''''传入整数型字符串如“0,1,2,3,4”和下标,使下标位整数+1 Public function AddrCountAdd(strSrc,inttag) val=GetCount(strSrc,inttag) AddrCountAdd=EditAddrStr(strSrc,inttag,val+1) end function Public function FirstAdd(inttag) arrayTemp=split(strProvince,",") for i=0 to Ubound(arrayTemp) if i=inttag then arrayTemp(i)=1 else arrayTemp(i)=0 end if next FirstAdd=join(arrayTemp,",") end function Public Sub CreatePic(strIntArray) wordArray=split(strProvince,",") intArray=split(strIntArray,",") intTotal=0 for each i in intArray intTotal=intTotal+i next %> <table width="450" cellpadding="1" cellspacing="1" bgcolor="#333333"> <tr bgcolor="#FFFFFF"> <td width="100" align="center"><input type="button" onClick="javascript :window.close()" value="关闭窗口"></td> <td align="right" width="300">总数:</td> <td widht="50"><%=intTotal%></td> <td widht="50">百分比</td> </tr> <%for j=0 to Ubound(wordArray)%> <tr bgcolor="#FFFFFF"> <td align="center"><%=wordArray(j)%></td> <td> <table width="<%=cint((intArray(j)/intTotal)*299)%>" border="0" cellspacing="2" cellpadding="2" bgcolor="#333333"> <tr> <td height="10"></td> </tr> </table> </td> <td><%=intArray(j)%></td> <td><%=int((intArray(j)/intTotal)*10000)/100%>%</td> </tr> <% next %> </table> <% end sub end class %> 统计代码(项目的核心代码): <!--#include file="inc/setup.asp"--> <% ''''''''30秒防刷功能 if session("ADCount")="" then session("ADCount")=1 else if request("jumpUrl")<>"" then response.Redirect(request("jumpUrl")) response.end() end if if request.Cookies("ADCount")="" then response.Cookies("ADCount")=now() else if DateDiff("s",request.Cookies("ADCount"),now())>Setup_refresh then response.Cookies("ADCount")=now() else response.Redirect(request("jumpUrl")) response.end() end if end if ''''''''1.得到访问广告的id和跳转地址,并获得广告所有者id ''''''''2.获得访问者的ip ''''''''3.根据ip得到一级地址,2级地址,及省代号. ''''''''4,得到年,月,日代号 ''''''''5,ADList表,点击+1,admin表点击+1。 ''''''''6,根据小时代号,ADCount表点击+1或产生新记录 '''''''' 6.1产生年记录字段,年记录中,月与日期为0,小时字段为24 '''''''' 6.2产生月记录字段,月记录中,日期字段为0,小时字段为24 '''''''' 6.3产生日记录字段,日记录中,小时字段为24 '''''''' 6.4产生小时记录字段,小时记录中,所有字段不为0,小时字段不为24 ''''''''7,根据小时代号,UserCount表点击+1或产生新的记录 '''''''' 7.1产生年记录字段,年记录中,月与日期为0,小时字段为24 '''''''' 7.2产生月记录字段,月记录中,日期字段为0,小时字段为24 '''''''' 7.3产生日记录字段,日记录中,小时字段为24 '''''''' 7.4产生小时记录字段,小时记录中,所有字段不为0,小时字段不为24 ''''''''8,广告日志表+1条记录 dim id,jumpUrl,logtable,strIP,lngIP,addr1,addr2,intPro,intYear,intMonth,intDay,intHour,intUsrID ''''''''获得需变量,完成步骤1-4 id=request("id") ''''''''得到广告列表id intUsrID=GetUsrID(id) ''''''''得到用户id jumpUrl=request("jumpUrl") ''''''''得到跳转地址 logtable="log"&id ''''''''得到产生的表名 set OpIP =new IP set AC=new AddrCount OpIP.GetDetails() strIP=OpIP.GetStrIP() ''''''''得到字符串ip lngIP=OpIP.GetLongIP() ''''''''得到长整形ip addr1=OpIP.Addr() ''''''''得到大概地址 addr2=OpIP.AddrDetails() ''''''''得到详细地址 intPro=AC.GetAddrNo(OpIP.AddrProvince) ''''''''得到省号 intYear=Year(now()) ''''''''得到年号 intMonth=Month(now()) ''''''''得到月号 intDay=Day(now()) ''''''''得到日期号 intHour=Hour(now()) ''''''''得到小时号 set OpIP=nothing conn.execute("update ADList set AllCount=AllCount+1 where id="&id) ''''''''完成步骤5,广告列表+1 conn.execute("update admin set AllCount=AllCount+1 where id="&intUsrID)''''''''完成步骤5,用户总点击列表+1 ''''''''完成步骤6 call Step6(intYear,0,0,24) ''''''''年访问量记录 call Step6(intYear,intMonth,0,24) ''''''''月访问量记录字段 call Step6(intYear,intMonth,intDay,24) ''''''''日访问量记录字段 call Step6(intYear,intMonth,intDay,intHour) ''''''''小时记录字段 ''''''''完成步骤7 call Step7(intYear,0,0,24) ''''''''年访问量记录 call Step7(intYear,intMonth,0,24)''''''''月访问量记录字段 call Step7(intYear,intMonth,intDay,24) ''''''''日访问量字段 call Step7(intYear,intMonth,intDay,intHour) ''''''''小时记录字段 ''''''''完成步骤8 if HaveTable(logtable) then conn.execute("insert into "&logtable&" (ip,intip,logtime,addj,addf,area) values(''''''''"&strIP&"'''''''',''''''''"&lngIP&"'''''''',''''''''"&now()&"'''''''',''''''''"&addr1&"'''''''',''''''''"&addr2&"'''''''',''''''''"&intPro&"'''''''')") conn.close() if request("jumpUrl")<>"" then response.Redirect(jumpUrl) sub Step6(stryear,strmonth,strday,strhour) SET rs=SERVER.CREATEOBJECT("ADODB.RECORDSET") sql="select * from ADCount where ADid="&id&" and YearNo="&stryear&" and MonthNo="&strmonth&" and DayNo="&strday&" and HourNo="&strhour rs.open sql,conn,1,3 if not rs.eof then rs("DayCount")=rs("DayCount")+1 rs("Area")=AC.AddrCountAdd(rs("Area"),intPro) rs.update else rs.addnew rs("ADid")=id rs("YearNo")=stryear rs("MonthNo")=strmonth rs("DayNo")=strday rs("HourNo")=strhour rs("DayCount")=1 rs("Area")=AC.FirstAdd(intPro) rs.update end if set rs=nothing end sub sub Step7(stryear,strmonth,strday,strhour) SET rs=SERVER.CREATEOBJECT("ADODB.RECORDSET") sql="select * from UserCount where UserID="&intUsrID&" and MonthNo="&strmonth&" and YearNo="&stryear&" and DayNo="&strday&" and HourNo="&strhour rs.open sql,conn,1,3 if not rs.eof then rs("DayCount")=rs("DayCount")+1 rs("Area")=AC.AddrCountAdd(rs("Area"),intPro) rs.update else rs.addnew rs("UserID")=intUsrID rs("YearNo")=stryear rs("MonthNo")=strmonth rs("DayNo")=strday rs("HourNo")=strhour rs("DayCount")=1 rs("Area")=AC.FirstAdd(intPro) rs.update end if set rs=nothing end sub function GetUsrID(id) set rs2=conn.execute("select UsrID from ADList where id="&id) if not rs2.eof then GetUsrID=rs2("UsrID") set rs2=nothing end function %>
相关标签:流量统计系统 asp类方法
|
|
|