asp動態(tài)程序防采集的方法

字號:


    昨天在網(wǎng)上看到一個防采集軟件,說采集只訪問當(dāng)前網(wǎng)頁,不會訪問網(wǎng)頁的圖片、JS等,今天突然想到,通過動態(tài)程序和Js訪問分別記錄訪問者的IP,然后進(jìn)行IP判斷,由于采集過程不會訪問JS,采集的時候只會查到用動態(tài)程序記錄的IP,而不會有通過JS記錄的IP,從而實現(xiàn)網(wǎng)頁程序的防采集。
    防采集的原理非常簡單,首先放一段動態(tài)語句,把訪問者的IP加入到數(shù)據(jù)庫的一個表里,然后在頁面底部加入一個JS,JS直接訪問動態(tài)頁面,將訪問者的IP加入到數(shù)據(jù)庫的另外一個表里。再次訪問的時候,從兩個表里讀IP數(shù)據(jù),然后判斷時間差,如果只在第一個表里找到,在第二個表里找不到,或者時間差超過10秒,則認(rèn)為是采集。
    優(yōu)點
    1.部署簡單,只要是動態(tài)語言就能很容易的實現(xiàn),無需借助服務(wù)器端程序
    2.殺傷力大,幾乎能封殺所有的采集過程
    缺點
    1.第一個缺點還是殺傷力大,如果需要實際使用需要考慮一些特殊情況,以免誤殺已經(jīng)殺掉搜索爬蟲
    2.只適用于動態(tài)網(wǎng)頁,靜態(tài)頁面就沒法用了
    流程寫的比較亂,不過原理本身就不是很復(fù)雜,下面附上程序例子,懂ASP的應(yīng)該很快就能看懂。
    程序例子(ASP+ACCESS)(測試程序下載):
    1.建立數(shù)據(jù)庫
    表1:Ip1,字段Ip1_Adderss(文本),Ip1_Time(日期/時間,默認(rèn)值=Now())
    表2:Ip2,字段Ip2_Adderss(文本),Ip2_Time(日期/時間,默認(rèn)值=Now())
    2.Index.asp(僅動態(tài)代碼,全部代碼請見測試程序中)
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
    <%
    Dim Conn,Rs,Sqlstr,Ip,IpTime,IpTime2,NewUser
    NewUser=0
    Set Conn = Server.CreateObject("Adodb.Connection")
    Set Rs=Server.Createobject("Adodb.RecordSet")
    ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("Data.mdb")
    Conn.Open ConnStr
    Ip=Request.ServerVariables("REMOTE_ADDR")
    Sqlstr="Select * From [Ip1] Where Ip1_Address='"&Ip&"' Order By Ip1_Id Desc"
    Rs.Open Sqlstr,Conn,1,3
    If Rs.Eof Then
    NewUser=1
    Application.Lock()
    Rs.AddNew()
    Rs("Ip1_Address")=Ip
    Rs.Update()
    Application.UnLock()
    Else
    IpTime=Rs("Ip1_Time")
    Application.Lock()
    Rs.AddNew()
    Rs("Ip1_Address")=Ip
    Rs.Update()
    Application.UnLock()
    End If
    Rs.Close
    If NewUser=0 Then
    Sqlstr="Select * From [Ip2] Where Ip2_Address='"&Ip&"' Order By Ip2_Id Desc"
    Rs.Open Sqlstr,Conn,1,3
    If Rs.Eof Then
    Rs.Close
    Response.Write("請勿采集!")
    Response.End()
    Else
    IpTime2=Rs("Ip2_Time")
    If DateDiff("s",IpTime2,IpTime)>10 Then
    Rs.Close
    Response.Write("請勿采集!")
    Response.End()
    End If
    End If
    Rs.Close
    End If
    %>
    3.Js.asp
    <%
    Dim Conn,Rs,Sqlstr,Ip
    Set Conn = Server.CreateObject("Adodb.Connection")
    Set Rs=Server.Createobject("Adodb.RecordSet")
    ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("Data.mdb")
    Conn.Open ConnStr
    Ip=Request.ServerVariables("REMOTE_ADDR")
    Sqlstr="Select * From [Ip2]"
    Rs.Open Sqlstr,Conn,1,3
    Application.Lock()
    Rs.AddNew()
    Rs("Ip2_Address")=Ip
    Rs.Update()
    Application.UnLock()
    Rs.Close
    %>
    4.Get.asp
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
    <%
    Response.Write(Server.HTMLEncode(GetHttpPage("http://localhost/Index.asp","GB2312")))
    '==============================
    '函 數(shù) 名:GetHttpPage
    '作 用:獲取頁面源代碼函數(shù)
    '參 數(shù):網(wǎng)址HttpUrl
    '==============================
    Function GetHttpPage(HttpUrl,Code)
    If IsNull(HttpUrl)=True Or HttpUrl="" Then
    GetHttpPage="A站點維護(hù)中!"
    Exit Function
    End If
    On Error Resume Next
    Dim Http
    Set Http=server.createobject("MSX"&"ML2.XML"&"HTTP")
    Http.open "GET",HttpUrl,False
    Http.Send()
    If Http.Readystate<>4 then
    Set Http=Nothing
    GetHttpPage="B站點維護(hù)中!"
    Exit function
    End if
    GetHttpPage=BytesToBSTR(Http.responseBody,Code)
    Set Http=Nothing
    If Err.number<>0 then
    Err.Clear
    GetHttpPage="C站點維護(hù)中!"
    Exit function
    End If
    End Function
    '==============================
    '函 數(shù) 名:BytesToBstr
    '作 用:轉(zhuǎn)換編碼函數(shù)
    '參 數(shù):字符串Body,編碼Cset
    '==============================
    Function BytesToBstr(Body,Cset)
    Dim Objstream
    Set Objstream = Server.CreateObject("ado"&"d"&"b.st"&"re"&"am")
    Objstream.Type = 1
    Objstream.Mode =3
    Objstream.Open
    Objstream.Write body
    Objstream.Position = 0
    Objstream.Type = 2
    Objstream.Charset = Cset
    BytesToBstr = Objstream.ReadText
    Objstream.Close
    set Objstream = nothing
    End Function
    %>