How to obtain the real IP address of a user

Source: Internet
Author: User

I. No proxy server is used:

REMOTE_ADDR = your IP address

HTTP_VIA = no value or no Display

HTTP_X_FORWARDED_FOR = no value or no Display

 

II. Transparent Proxy Server: Transparent Proxies

REMOTE_ADDR = IP address of the last proxy server

HTTP_VIA = proxy server IP address

HTTP_X_FORWARDED_FOR = your real IP address. When multiple proxy servers are used, this value is similar to the following: 203.98.1820.3, 203.98.1820.3, 203.129.72.215.

This type of proxy server still forwards your information to your access object, which cannot hide your real identity.

 

III. Normal Anonymous proxy server: Anonymous Proxies

REMOTE_ADDR = IP address of the last proxy server

HTTP_VIA = proxy server IP address

HTTP_X_FORWARDED_FOR = proxy server IP address. When multiple proxy servers are used, this value is similar to the following: 203.98.1820.3, 203.98.1820.3, 203.129.72.215.

Your real IP address is hidden, but you are disclosed to the access object that you use the proxy server to access them.

 

IV. Destorting Proxies

REMOTE_ADDR = proxy server IP address

HTTP_VIA = proxy server IP address

HTTP_X_FORWARDED_FOR = random IP address. When multiple proxy servers are used, the value is as follows: 203.98.182.163, 203.98.182.163, 203.129.72.215.

It tells the access object that you used the proxy server, but fabricated a false random IP address instead of your real IP address to cheat it.

 

5. High Anonymity Proxies (Elite proxies)

REMOTE_ADDR = proxy server IP address

HTTP_VIA = no value or no Display

HTTP_X_FORWARDED_FOR = no value or no value is displayed. When multiple proxy servers are used, the value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

Instance

/**
* Obtain the real IP address of the user.
*
* @ Access public
* @ Return string
*/
Function real_ip ()
{
// Initialize a variable $ realip
Static $ realip = NULL;
// If $ realip is not true equal to NULL, return
If ($ realip! = NULL)
    {
Return $ realip;
    }
// If $ _ SERVER has a value
If (isset ($ _ SERVER ))
    {
// If $ _ SERVER ['http _ X_FORWARDED_FOR '] has a value
// Indicates that the client accesses the Internet through proxy
If (isset ($ _ SERVER ['http _ X_FORWARDED_FOR '])
        {
// Use The explode () function to split the ',' into arrays
$ Arr = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']);
/* Obtain the first non-unknown valid IP string in X-Forwarded-*/
// Start traversing the array
Foreach ($ arr AS $ ip)
            {
// Remove the leading and trailing spaces
$ Ip = trim ($ ip );
// Either unknown or the real Internet address, save the value and exit the loop
If ($ ip! = 'Unknown ')
                {
$ Realip = $ ip;
Break;
                }
            }
        }
// $ _ SERVER ['http _ X_FORWARDED_FOR '] has no value and
// $ _ SERVER ['http _ CLIENT_IP '] has a value and takes the value as the real IP address.
Elseif (isset ($ _ SERVER ['http _ CLIENT_IP '])
        {
$ Realip = $ _ SERVER ['http _ CLIENT_IP '];
        }
// $ _ SERVER ['http _ X_FORWARDED_FOR '] has no value (not used as a proxy to access the Internet) and
// $ _ SERVER ['http _ CLIENT_IP '] has no value
Else
        {
// If $ _ SERVER ['remote _ ADDR '] has a value, use the value as the real IP address.
If (isset ($ _ SERVER ['remote _ ADDR '])
            {
$ Realip = $ _ SERVER ['remote _ ADDR '];
            }
No else value is returned. '0. 0.0.0'
            {
$ Realip = '0. 0.0.0 ';
            }
        }
    }
// $ _ SERVER has no value
Else
    {
// If getenv ('http _ X_FORWARDED_FOR ') is not empty, use its value as the real IP address.
If (getenv ('http _ X_FORWARDED_FOR '))
        {
$ Realip = getenv ('http _ X_FORWARDED_FOR ');
        }
// If getenv ('http _ CLIENT_IP ') is not empty, use its value as the real IP address.
Elseif (getenv ('http _ CLIENT_IP '))
        {
$ Realip = getenv ('http _ CLIENT_IP ');
        }
// Otherwise, take the value of getenv ('remote _ ADDR ') as the real IP address.
Else
        {
$ Realip = getenv ('remote _ ADDR ');
        }
    }
Preg_match ("/[d.] {7, 15}/", $ realip, $ onlineip); // why is this sentence used? Please kindly advise.
$ Realip =! Empty ($ onlineip [0])? $ Onlineip [0]: '0. 0.0.0 '; // why is this sentence used? Please advise.
Return $ realip;
}


Recently, a problem occurred when obtaining the IP address of the browser client. The main phenomenon is that the IP address of a client using Beijing Telecom in Wuhan cannot be accessed by my program, I checked some information and modified it. The main difference between the three attributes (HTTP_X_FORWARDED_FOR, HTTP_VIA, and REMOTE_ADDR) of the IP address obtained by c # is analyzed as follows:

I. No proxy server is used:

REMOTE_ADDR = your IP address
HTTP_VIA = no value or no Display
HTTP_X_FORWARDED_FOR = no value or no Display

II. Transparent Proxy Server: Transparent Proxies

REMOTE_ADDR = IP address of the last proxy server
HTTP_VIA = proxy server IP address
HTTP_X_FORWARDED_FOR = your real IP address. When multiple proxy servers are used, this value is similar to the following: 203.98.1820.3, 203.98.1820.3, 203.129.72.215.

This type of proxy server still forwards your information to your access object, which cannot hide your real identity.

III. Normal Anonymous proxy server: Anonymous Proxies

REMOTE_ADDR = IP address of the last proxy server
HTTP_VIA = proxy server IP address
HTTP_X_FORWARDED_FOR = proxy server IP address. When multiple proxy servers are used, this value is similar to the following: 203.98.1820.3, 203.98.1820.3, 203.129.72.215.

Your real IP address is hidden, but you are disclosed to the access object that you use the proxy server to access them.

IV. Destorting Proxies

REMOTE_ADDR = proxy server IP address
HTTP_VIA = proxy server IP address
HTTP_X_FORWARDED_FOR = random IP address. When multiple proxy servers are used, the value is as follows: 203.98.182.163, 203.98.182.163, 203.129.72.215.

It tells the access object that you used the proxy server, but fabricated a false random IP address instead of your real IP address to cheat it.

5. High Anonymity Proxies (Elite proxies)

REMOTE_ADDR = proxy server IP address
HTTP_VIA = no value or no Display
HTTP_X_FORWARDED_FOR = no value or no value is displayed. When multiple proxy servers are used, the value is similar to the following: 203.98.182.163, 203.98.182.163, 203.129.72.215.

The proxy server information replaces all your information, just as you directly access the object using the proxy server.

For the above property analysis, change the code:

 

/// <Summary>
/// Obtain the client Ip address
/// </Summary>
/// <Returns> </returns>
Public string GetUserIP ()
        {
String _ userIP;
Try
            {
If (HttpContext. Current. Request. ServerVariables ["HTTP_VIA"] = null)
                {
_ UserIP = HttpContext. Current. Request. UserHostAddress;
                }
Else
                {
_ UserIP = HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"];
                }
            }
Catch (Exception)
            {
_ UserIP = "unable to obtain this IP address ";
            }
Return _ userIP;
        }

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.