We generally use $ _ SERVER [REMOTE_ADDR] to obtain the user's IP address. after my research, there are two methods to obtain the user's real internet IP address. Generally, $ _ SERVER ['remote _ ADDR '] is used to obtain the user IP address,
However, if a reverse proxy is used, REMOTE_ADDR in the HTTP header is not the user's address, but the address of the previous proxy.
After my research, there are two ways to obtain the real Internet IP address of a user.
Method 1: curl
The code is as follows:
Function get_onlineip (){
$ Ch = curl_init ('http: // www.ip138.com/ip2city.asp ');
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ A = curl_exec ($ ch );
Preg_match ('/\ [(. *) \]/', $ a, $ ip );
Return $ ip [1];
}
Method 2: $ _ SERVER ['http _ X_FORWARDED_FOR '] to obtain the corresponding address
The code is as follows:
Function get_onlineip (){
$ Onlineip = '';
If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR'), 'Unknown ')){
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'Unknown ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], 'Unknown ')){
$ Onlineip = $ _ SERVER ['remote _ ADDR '];
}
Return $ onlineip;
}