Get the user's ip address

In the global magic variable $_SERVER, there are three quantities that store ip

First:

HTTP_X_FORWARDED_FOR has clientip, proxy1, proxy2.... proxy ip. Use commas to separate. So use him to get the user's ip.

the second:

HTTP_CLIENT_IP This is a possible ip

The third:

REMOTE_ADDR can be disguised by a proxy, but this ip is the server that is actually connecting to your server.

Get the ip in the order above

The code to get the ip is as follows:

         $ip = '';
         if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], 'unknown')) {
             $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
             strpos($ip, ',') && list($ip) = explode(',', $ip);
         } else if (!empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], 'unknown')) {
             $ip = $_SERVER['HTTP_CLIENT_IP'];
         } else if (!empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
             $ip = $_SERVER['REMOTE_ADDR'];
         }
         return $ip;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324778658&siteId=291194637