根据IP判断城市

asp

2008-05-31 15:17

<%
   
    '如果提示@ 命令只可以在 Active Server Page 中使用一次 错误,请把上面一行代码去掉

    if Request.ServerVariables("HTTP_X_FORWARDED_FOR")="" then
    zhenip=Request.ServerVariables("REMOTE_ADDR")
    else
    zhenip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    end if
   

    function ax_IPxianZHI(sip)
        If isnumeric(left(sip,2)) Then
            If sip="127.0.0.1" Then sip="192.168.0.1"
            str1=left(sip,instr(sip,".")-1)
            sip=mid(sip,instr(sip,".")+1)
            str2=left(sip,instr(sip,".")-1)
            sip=mid(sip,instr(sip,".")+1)
            str3=left(sip,instr(sip,".")-1)
            str4=mid(sip,instr(sip,".")+1)
            If isNumeric(str1)=0 Or isNumeric(str2)=0 Or isNumeric(str3)=0 Or isNumeric(str4)=0 Then

            Else
                num=cint(str1)*256*256*256+cint(str2)*256*256+cint(str3)*256+cint(str4)-1
                Dim adb,aConnStr,AConn
                adb = "axIP.mdb"      '解压后的数据库(可以自行更改名或路径)
                aConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(adb)
                Set AConn = Server.CreateObject("ADODB.Connection")
                aConn.Open aConnStr
                sql="select country,city from address where ip1 <="&num&" and ip2 >="&num
                Set irs=AConn.Execute(sql)
                If irs.eof And irs.bof Then
                    ax_IPxianZHI=template.Strings(12)
                Else
                    Do While Not irs.eof
                        ax_IPxianZHI=ax_IPxianZHI&irs(0) & irs(1)
                    irs.movenext
                    Loop
                End If
                irs.close
                Set irs=nothing
                AConn.Close
                Set AConn=Nothing
            End If
        Else
            ax_IPxianZHI=template.Strings(12)
        End If
        Response.Write(ax_IPxianZHI)
    end function
'上面代码可以放到页面最上面
call ax_IPxianZHI(zhenip)
%>