当前位置: 代码迷 >> ASP >> 怎么取得本机IP
  详细解决方案

怎么取得本机IP

热度:215   发布时间:2012-02-14 19:19:19.0
如何取得本机IP
本机IP为192.168.1.13,电脑通过路由器上网,路由器拔号后IP为125.90.232.125,需要在一页面得出内外网IP,但始终都没办法得到我本机IP地址,我用的方法如下:
Request.ServerVariables   ( "REMOTE_ADDR "),这个取出了125.90.232.125
但不知192.168.1.13也就是我本机的IP如何取出,请大家指教

------解决方案--------------------
如果客户端是使用代理服务器来访问,那取到的就是代理服务器的 IP 地址,而不是真正的客户端 IP 地址。要想透过代理服务器取得客户端的真实 IP 地址,就要使用quest.ServerVariables( "HTTP_X_FORWARDED_FOR ") 来读取。
  不过如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables( "HTTP_X_FORWARDED_FOR ") 取到的值将是空的。因此这样处理:


userip = Request.ServerVariables( "HTTP_X_FORWARDED_FOR ")
If userip = " " Then userip = Request.ServerVariables( "REMOTE_ADDR ")



------解决方案--------------------
同意,也可以更详细点
Function GetUserIP() '获取用户真实IP
Dim sIP
If Request.ServerVariables( "HTTP_X_FORWARDED_FOR ") = " " OR InStr(Request.ServerVariables( "HTTP_X_FORWARDED_FOR "), "unknown ") > 0 Then
sIP = Request.ServerVariables( "REMOTE_ADDR ")
ElseIf InStr(Request.ServerVariables( "HTTP_X_FORWARDED_FOR "), ", ") > 0 Then
sIP = Mid(Request.ServerVariables( "HTTP_X_FORWARDED_FOR "), 1, InStr(Request.ServerVariables( "HTTP_X_FORWARDED_FOR "), ", ")-1)
ElseIf InStr(Request.ServerVariables( "HTTP_X_FORWARDED_FOR "), "; ") > 0 Then
sIP = Mid(Request.ServerVariables( "HTTP_X_FORWARDED_FOR "), 1, InStr(Request.ServerVariables( "HTTP_X_FORWARDED_FOR "), "; ")-1)
Else
sIP = Request.ServerVariables( "HTTP_X_FORWARDED_FOR ")
End If
GetUserIP = CheckIP(sIP)
End Function

Function CheckIP(sIP)
sIP=Trim(sIP)
sIP=Replace(sIP, ". ", ", ")
sIP=ChkIDs(sIP)
If sIP <> " " Then sIP=Replace(sIP, ", ", ". ")
CheckIP=sIP
End Function

Function ChkIDs(byval sIDs)
Dim aIDs,i,sReturn
sIDs=Trim(sIDs)
If Len(sIDs)=0 Then Exit Function
aIDs=Split(sIDs, ", ")
For i=0 To Ubound(aIDs)
'发现任意不符合的字符,直接跳出
If Not IsNumeric(aIDs(i)) Then
Exit Function
Else
sReturn=sReturn & ", " & Int(aIDs(i))
End If
Next
If Left(sReturn,1)= ", " Then sReturn=Right(sReturn,Len(sReturn)-1)
ChkIDs=sReturn
sReturn= " "
End Function

  相关解决方案