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: curlCopyCodeThe 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 copy Code 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 '];
}< br> return $ onlineip;
}