Get User IP address in ASP our easiest way to get here is to use the
The code is as follows |
Copy Code |
Request.ServerVariables ("REMOTE_ADDR") |
However, if the user uses the proxy server IP address is not correct, but we can pass
The code is as follows |
Copy Code |
Request.ServerVariables ("REMOTE_ADDR") |
Results The above two methods of obtaining IP can be operated as follows
The code is as follows |
Copy Code |
Userip = Request.ServerVariables ("Http_x_forwarded_for") If Userip = "" Then Userip = Request.ServerVariables ("REMOTE_ADDR") |
The above is just a simple method of judging, in the actual application we may encounter more cases, the following look a good example
The code is as follows |
Copy Code |
<%private Function getip () Dim stripaddr If request.servervariables ("http_x_forwarded_for") = "" OR InStr (Request.serverv Ariables ("Http_x_forwarded_for"), "Unknown") > 0 Then stripaddr = Request.ServerVariables ("remote_addr") ElseIf InStr (Request.ServerVariables ("Http_x_forwarded_for"), ",") > 0 Then stripaddr = 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 stripaddr = Mid (request.server Variables ("Http_x_forwarded_for"), 1, InStr (Request.ServerVariables ("Http_x_forwarded_for"), ";") 1) Else Stripaddr = Request.ServerVariables ("http_x_forwarded_for") End If GetIP = Trim (Mid stripaddr , 1) End Function Ip=getip () Response.Write (IP) %> |
This can almost get the user's real IP address, but also can not save all the agents are able to obtain, but we all use the above method.
If you have friends who use ASP.net, refer to the following methods to obtain
The code is as follows |
Copy Code |
Method One HttpContext.Current.Request.UserHostAddress; Method Two httpcontext.current.request.servervariables["REMOTE_ADDR"]; Method Three String strhostname = System.Net.Dns.GetHostName (); String clientipaddress = System.Net.Dns.GetHostAddresses (strhostname). GetValue (0). ToString (); Method IV (ignoring the agent) httpcontext.current.request.servervariables["Http_x_forwarded_for"]; Method Five var ip = ' <!--#echo var= ' remote_addr '--> '; Alert ("Your IP address is" +IP); //Method VI (ignoring proxy) function getlocalipaddress () { var obj = null; var rslt = ""; try { obj = new Activexob Ject ("Rcbdyctl.") Setting "); rslt = obj. getipaddress; obj = null; } catch (E) { // } return rslt; } |