1 <? Php
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 <? Php
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