PHP obtains the client IP address

Source: Internet
Author: User
1 & lt ;? Php2 // Exampleuseofgetenv () 3 $ ipgetenv (& amp; #39; REMOTE_ADDR & amp; #39;); 4 // OrsimplyuseaSuperglobal ($ _ SERVERor $ _ ENV) 5 $ ip $ _ SERVER [& amp; #39;

1

2 // Example use of getenv ()

3 $ ip = getenv ('remote _ ADDR ');

4 // Or simply use a Superglobal ($ _ SERVER or $ _ ENV)

5 $ ip = $ _ SERVER ['remote _ ADDR '];

6?>


This is the method provided by manual, the official PHP website.

However, when the Web server API is ASAPI (IIS), the getenv function does not work. In this case, if you use getenv to obtain the ip address of the client, the ip address is incorrect.

Therefore, the safer and more accurate method is to avoid using the getenv function. For example, you can use the following function to obtain ip information:


01

02 function GetIP (){

03 if (getenv ("HTTP_CLIENT_IP ")

04 & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))

05 $ ip = getenv ("HTTP_CLIENT_IP ");

06 else if (getenv ("HTTP_X_FORWARDED_FOR ")

07 & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown "))

08 $ ip = getenv ("HTTP_X_FORWARDED_FOR ");

09 else if (getenv ("REMOTE_ADDR ")

10 & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))

11 $ ip = getenv ("REMOTE_ADDR ");

12 else if (isset ($ _ SERVER ['remote _ ADDR '])

13 & $ _ SERVER ['remote _ ADDR ']

14 & strcasecmp ($ _ SERVER ['remote _ ADDR '], "unknown "))

15 $ ip = $ _ SERVER ['remote _ ADDR '];

16 else

17 $ ip = "unknown ";

18 return ($ ip );

19}

20

21 $ ip = GetIP ();

22 echo $ ip;

23?>


The differences between HTTP_X_FORWARDED_FOR, HTTP_VIA, and REMOTE_ADDR are:


I. no proxy server is used:

REMOTE_ADDR = your IP address
HTTP_VIA = no value or www.2cto.com is not displayed
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.


Function getip (){
If ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ Online_ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
} Elseif ($ _ SERVER ['http _ CLIENT_IP ']) {
$ Online_ip = $ _ SERVER ['http _ CLIENT_IP '];
} Else {
$ Online_ip = $ _ SERVER ['remote _ ADDR '];
}
Return $ online_ip;
}
 

From PPP

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.