您所在的位置:小祥子 » 编程 » ASP » 正文

ASP下载类、下载页面实现代码

时间:2015-02-22 编辑:本站 来源:本站原创

这是一个独立的下载页,通过下载详情页点击过来,只需要传一个ID参数,然后这个页面处理跳转真实下载地址。

类与实例化类在同一个页面,实际上也不需要专门做这么一个类,我认为。本来是来自其他下载程序,但为了让代码可以用于其他程序,我对这个页面的代码进行了一些处理。比如实例化其他调用文件里的类。

以下代码基本只需要修改表、字段的名称就可以使用了。

<!-- #include file="conn.ASP" -->
<% dim obj
set obj = New Download
 obj.AspInit()
set obj = nothing

Class Download

 Private ID, i
 Private Sub Class_Initialize()
 End Sub
 Private Sub Class_Terminate()
 End Sub
 Public Sub AspInit() '入口判断是否不为空或不为数字
 
  If Request.QueryString("ID") = "" Then
   Response.Write "非法操作!指定的参数不正确。"
   Response.End
  ElseIf IsNumeric(Request.QueryString("ID")) = False Then
   Response.Write "非法操作!指定的参数不正确。"
   Response.End
  Else
   ID = CLng(Request.QueryString("ID"))
  End If
  Call Main
 
 End Sub
 Private Sub Main() '主过程,判断来路页面
   Dim strReferer, isHttp, strHostName ‘strHostName为网站主域名
   isHttp = False
   strReferer = Request.ServerVariables("HTTP_REFERER")
   For i = 0 To UBound(strHostName)
    If InStr(strReferer, Trim(strHostName(i))) > 0 Then isHttp = True
   Next
   If IsNull(strReferer) Or isHttp = False Then
    Response.Write "非法操作!系统启用防盗链机制,你的下载链接来自于非法盗链页面,请访问<a href=""/"">本站页面</a>下载软件。"
    Exit Sub
   End If
  'End If
  
  Dim Rs, ResID, SiteID, URL
  Set Rs = conn.Execute("select * from Down_Address where ID=" & ID)
  If Rs.EOF And Rs.BOF Then
   Response.Write ("Sorry,下载地址无效,可能已经被删除!")
   Set Rs = Nothing
   Exit Sub
  Else
   ResID = Rs("ResourceID")
   SiteID = Rs("SiteID")
   URL = Rs("URL")
  End If
  Set Rs = Nothing
  Call UpdateHits(ResID)
  If Trim(Request.QueryString("URL")) <> "" Then
   Response.Redirect (Trim(Request.QueryString("URL")))
  Else
   Response.Redirect (GetURL(SiteID, URL))
  End If
 End Sub
 
 Private Sub UpdateHits(ResID)'更新
  Dim Rs
  Set Rs = conn.Execute("select HitsTotal,DateMonth,DateWeek,DateToday from Down_Resource where ID=" & ResID)
  '更新软件下载次数
  conn.Execute ("update Down_Address set Hits=Hits+1 where ID=" & ID)
  conn.Execute ("update Down_Resource set HitsTotal=HitsTotal+1,HitsMonth=HitsMonth+1,HitsWeek=HitsWeek+1,HitsToday=HitsToday+1 where ID=" & ResID)
  If DateDiff("H", Rs(3), Now) >= 24 Then conn.Execute ("update Down_Resource set DateToday=" & SqlNowString & ",HitsToday=1 where ID=" & ResID)
  If DateDiff("D", Rs(2), Now) >= 8 Then conn.Execute ("update Down_Resource set DateWeek=" & SqlNowString & ",HitsWeek=1 where ID=" & ResID)
  If DateDiff("D", Rs(1), Now) >= 31 Then conn.Execute ("update Down_Resource set DateMonth=" & SqlNowString & ",HitsMonth=1 where ID=" & ResID)
  Set Rs = Nothing
 End Sub
 Private Function GetURL(SiteID, URL)
  If SiteID = 0 Then
   GetURL = URL
   Exit Function
  Else
   Dim Rs, arrDownloadName, arrDownloadURL, i
   Set Rs = conn.Execute("select ID,DownloadName,DownloadURL,isLB from Down_SiteSetting where ID = " & SiteID)
   arrDownloadName = Split(Rs(1), ",")
   arrDownloadURL = Split(Rs(2), ",")
   If Rs(3) Then '显示全部分站
    GetURL = arrDownloadURL(Trim(Request.QueryString("sID"))) & URL
   Else
    Randomize Timer
    i = Int(Rnd * UBound(arrDownloadName))
    GetURL = arrDownloadURL(i) & URL
   End If
   Set Rs = Nothing
  End If
 End Function

End Class %>