The method of obtaining the real IP address of the user by ASP

Source: Internet
Author: User
Tags httpcontext servervariables

General procedures have access to the user's IP address command, but we visit the site there are two kinds of our common users, one is the use of proxy IP Internet users, I would like to introduce the following.

 

Get User IP address in ASP our easiest way to get here is to use the

The code is as follows

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

Request.ServerVariables ("REMOTE_ADDR")

Results The above two methods of obtaining IP can be operated as follows

The code is as follows

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

  code is as follows &nbs P;

<%private Function GetIP ()  
Dim stripaddr 
If Request.ServerVariables ("http_x_forwarded_for") = "" OR InStr (Request.ServerVariables ("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.ServerVariables ("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

  code is as follows &nbs P;

//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 proxy)
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 ActiveXObject ("Rcbdyctl. Setting "); &nbsP
        rslt = obj. getipaddress; 
        obj = null; 
   } 
    catch (E)  
    { 
        //&NBSP
   } 
     
    return rslt; &NBSP
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.