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

Source: Internet
Author: User
Tags httpcontext servervariables

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;
}

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.