asp.net 中IP问题?
- 提问者网友:鼻尖触碰
- 2021-08-12 14:47
- 五星知识达人网友:舍身薄凉客
- 2021-08-12 16:14
建立一个asp文件,这里名字我取为getIP.asp,其内容如下:
<%
On Error Resume Next
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function
Function GetBody(ConStr,StartStr,OverStr,IncluL,IncluR)
If ConStr="$False$" or ConStr="" or IsNull(ConStr)=True Or StartStr="" or IsNull(StartStr)=True Or OverStr="" or IsNull(OverStr)=True Then
GetBody="$False$"
Exit Function
End If
Dim ConStrTemp
Dim Start,Over
ConStrTemp=Lcase(ConStr)
StartStr=Lcase(StartStr)
OverStr=Lcase(OverStr)
Start = InStrB(1, ConStrTemp, StartStr, vbBinaryCompare)
If Start<=0 then
GetBody="$False$"
Exit Function
Else
If IncluL=False Then
Start=Start+LenB(StartStr)
End If
End If
Over=InStrB(Start,ConStrTemp,OverStr,vbBinaryCompare)
If Over<=0 Or Over<=Start then
GetBody="$False$"
Exit Function
Else
If IncluR=True Then
Over=Over+LenB(OverStr)
End If
End If
GetBody=MidB(ConStr,Start,Over-Start)
End Function
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
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
%>
<%
ip=Request.ServerVariables("REMOTE_ADDR")
cc=split(ip,".")
ip2=cc(0)&"."&cc(1)&"."&cc(2)&".***"
url=" http://www.sogou.com/web?query="&ip&""
html=getHTTPPage(url)
dlwz=getBody(html,"地理位置"," ",false,false)
if dlwz="$False$" then
dlwz="未知区域"
end if
%>
然后在你主页需要的地方内插入这些代码:
<!-- #include file="getIP.asp"-->
<%
set fs=server.CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(server.MapPath("ip.txt"), 8, True)
f.write "【"
f.write "IP:"&ip
f.write "‖"
f.write "地理位置"&dlwz
f.write "】"
f.close
set f=nothing
set fs=nothing
%>
这样别人访问你的主页,IP和地址就写进本地一个叫ip的记事本里了。
我也是网上查的资料,自己应用了下,能实现
- 1楼网友:佘樂
- 2021-08-12 16:50
你是想知道对方所在的城市,是电信还是网通,这种信息吗?
如果不想自己弄一个IP库的话,就得临时去别的网站查询,
如果有自己的库的话,就是一个数据库查询,