Php: getting ip addres

I want to get ip addres visitors. could you tell me which element $_SERVER[]should i use?

$_SERVER['HTTP_CLIENT_IP'];
$_SERVER['HTTP_X_FORWARDED_FOR'];
or
$_SERVER['REMOTE_ADDR'];

thank

UPDATE:

If your client is connected to the Internet through a proxy server, then $ _SERVER ['REMOTE_ADDR'] in PHP simply returns the IP address of the proxy server, not the client machine. There is an additional server variable that may be available to determine the exact IP address of the client machine in PHP, these are HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR.

+3
source share
3 answers

$_SERVER['REMOTE_ADDR'];

According to PHP documentation : The IP address from which the user is viewing the current page.

IP-, ( ).
.

HTTP_X_FORWARDED_FOR - (, -), -. - - IP-; IP-.

HTTP_CLIENT_IP

+5

, - - . - , IP-.

+2

:

function getIP() {
  foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
    if (array_key_exists($key, $_SERVER) === true) {
        foreach (explode(',', $_SERVER[$key]) as $ip) {
           if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
              return $ip;
           }
        }
     }
   }
 }
+1

Source: https://habr.com/ru/post/1741099/


All Articles