Advertisement
krot

PMA_getIp

Mar 28th, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.76 KB | None | 0 0
  1.  function PMA_getIp()
  2.     {
  3.         global $REMOTE_ADDR;
  4.         global $HTTP_X_FORWARDED_FOR, $HTTP_X_FORWARDED, $HTTP_FORWARDED_FOR, $HTTP_FORWARDED;
  5.         global $HTTP_VIA, $HTTP_X_COMING_FROM, $HTTP_COMING_FROM;
  6.         global $HTTP_SERVER_VARS, $HTTP_ENV_VARS;
  7.  
  8.         // Get some server/environment variables values
  9.         if (empty($REMOTE_ADDR)) {
  10.             if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) {
  11.                 $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
  12.             }
  13.             else if (!empty($_ENV) && isset($_ENV['REMOTE_ADDR'])) {
  14.                 $REMOTE_ADDR = $_ENV['REMOTE_ADDR'];
  15.             }
  16.             else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['REMOTE_ADDR'])) {
  17.                 $REMOTE_ADDR = $HTTP_SERVER_VARS['REMOTE_ADDR'];
  18.             }
  19.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['REMOTE_ADDR'])) {
  20.                 $REMOTE_ADDR = $HTTP_ENV_VARS['REMOTE_ADDR'];
  21.             }
  22.             else if (@getenv('REMOTE_ADDR')) {
  23.                 $REMOTE_ADDR = getenv('REMOTE_ADDR');
  24.             }
  25.         } // end if
  26.         if (empty($HTTP_X_FORWARDED_FOR)) {
  27.             if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  28.                 $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
  29.             }
  30.             else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
  31.                 $HTTP_X_FORWARDED_FOR = $_ENV['HTTP_X_FORWARDED_FOR'];
  32.             }
  33.             else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'])) {
  34.                 $HTTP_X_FORWARDED_FOR = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'];
  35.             }
  36.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'])) {
  37.                 $HTTP_X_FORWARDED_FOR = $HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'];
  38.             }
  39.             else if (@getenv('HTTP_X_FORWARDED_FOR')) {
  40.                 $HTTP_X_FORWARDED_FOR = getenv('HTTP_X_FORWARDED_FOR');
  41.             }
  42.         } // end if
  43.         if (empty($HTTP_X_FORWARDED)) {
  44.             if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
  45.                 $HTTP_X_FORWARDED = $_SERVER['HTTP_X_FORWARDED'];
  46.             }
  47.             else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED'])) {
  48.                 $HTTP_X_FORWARDED = $_ENV['HTTP_X_FORWARDED'];
  49.             }
  50.             else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED'])) {
  51.                 $HTTP_X_FORWARDED = $HTTP_SERVER_VARS['HTTP_X_FORWARDED'];
  52.             }
  53.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_FORWARDED'])) {
  54.                 $HTTP_X_FORWARDED = $HTTP_ENV_VARS['HTTP_X_FORWARDED'];
  55.             }
  56.             else if (@getenv('HTTP_X_FORWARDED')) {
  57.                 $HTTP_X_FORWARDED = getenv('HTTP_X_FORWARDED');
  58.             }
  59.         } // end if
  60.         if (empty($HTTP_FORWARDED_FOR)) {
  61.             if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  62.                 $HTTP_FORWARDED_FOR = $_SERVER['HTTP_FORWARDED_FOR'];
  63.             }
  64.             else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED_FOR'])) {
  65.                 $HTTP_FORWARDED_FOR = $_ENV['HTTP_FORWARDED_FOR'];
  66.             }
  67.             else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_FORWARDED_FOR'])) {
  68.                 $HTTP_FORWARDED_FOR = $HTTP_SERVER_VARS['HTTP_FORWARDED_FOR'];
  69.             }
  70.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_FORWARDED_FOR'])) {
  71.                 $HTTP_FORWARDED_FOR = $HTTP_ENV_VARS['HTTP_FORWARDED_FOR'];
  72.             }
  73.             else if (@getenv('HTTP_FORWARDED_FOR')) {
  74.                 $HTTP_FORWARDED_FOR = getenv('HTTP_FORWARDED_FOR');
  75.             }
  76.         } // end if
  77.         if (empty($HTTP_FORWARDED)) {
  78.             if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
  79.                 $HTTP_FORWARDED = $_SERVER['HTTP_FORWARDED'];
  80.             }
  81.             else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED'])) {
  82.                 $HTTP_FORWARDED = $_ENV['HTTP_FORWARDED'];
  83.             }
  84.             else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_FORWARDED'])) {
  85.                 $HTTP_FORWARDED = $HTTP_SERVER_VARS['HTTP_FORWARDED'];
  86.             }
  87.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_FORWARDED'])) {
  88.                 $HTTP_FORWARDED = $HTTP_ENV_VARS['HTTP_FORWARDED'];
  89.             }
  90.             else if (@getenv('HTTP_FORWARDED')) {
  91.                 $HTTP_FORWARDED = getenv('HTTP_FORWARDED');
  92.             }
  93.         } // end if
  94.         if (empty($HTTP_VIA)) {
  95.             if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
  96.                 $HTTP_VIA = $_SERVER['HTTP_VIA'];
  97.             }
  98.             else if (!empty($_ENV) && isset($_ENV['HTTP_VIA'])) {
  99.                 $HTTP_VIA = $_ENV['HTTP_VIA'];
  100.             }
  101.             else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_VIA'])) {
  102.                 $HTTP_VIA = $HTTP_SERVER_VARS['HTTP_VIA'];
  103.             }
  104.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_VIA'])) {
  105.                 $HTTP_VIA = $HTTP_ENV_VARS['HTTP_VIA'];
  106.             }
  107.             else if (@getenv('HTTP_VIA')) {
  108.                 $HTTP_VIA = getenv('HTTP_VIA');
  109.             }
  110.         } // end if
  111.         if (empty($HTTP_X_COMING_FROM)) {
  112.             if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
  113.                 $HTTP_X_COMING_FROM = $_SERVER['HTTP_X_COMING_FROM'];
  114.             }
  115.             else if (!empty($_ENV) && isset($_ENV['HTTP_X_COMING_FROM'])) {
  116.                 $HTTP_X_COMING_FROM = $_ENV['HTTP_X_COMING_FROM'];
  117.             }
  118.             else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_COMING_FROM'])) {
  119.                 $HTTP_X_COMING_FROM = $HTTP_SERVER_VARS['HTTP_X_COMING_FROM'];
  120.             }
  121.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_COMING_FROM'])) {
  122.                 $HTTP_X_COMING_FROM = $HTTP_ENV_VARS['HTTP_X_COMING_FROM'];
  123.             }
  124.             else if (@getenv('HTTP_X_COMING_FROM')) {
  125.                 $HTTP_X_COMING_FROM = getenv('HTTP_X_COMING_FROM');
  126.             }
  127.         } // end if
  128.         if (empty($HTTP_COMING_FROM)) {
  129.             if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])) {
  130.                 $HTTP_COMING_FROM = $_SERVER['HTTP_COMING_FROM'];
  131.             }
  132.             else if (!empty($_ENV) && isset($_ENV['HTTP_COMING_FROM'])) {
  133.                 $HTTP_COMING_FROM = $_ENV['HTTP_COMING_FROM'];
  134.             }
  135.             else if (!empty($HTTP_COMING_FROM) && isset($HTTP_SERVER_VARS['HTTP_COMING_FROM'])) {
  136.                 $HTTP_COMING_FROM = $HTTP_SERVER_VARS['HTTP_COMING_FROM'];
  137.             }
  138.             else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_COMING_FROM'])) {
  139.                 $HTTP_COMING_FROM = $HTTP_ENV_VARS['HTTP_COMING_FROM'];
  140.             }
  141.             else if (@getenv('HTTP_COMING_FROM')) {
  142.                 $HTTP_COMING_FROM = getenv('HTTP_COMING_FROM');
  143.             }
  144.         } // end if
  145.  
  146.         // Gets the default ip sent by the user
  147.         if (!empty($REMOTE_ADDR)) {
  148.             $direct_ip = $REMOTE_ADDR;
  149.         }
  150.  
  151.         // Gets the proxy ip sent by the user
  152.         $proxy_ip     = '';
  153.         if (!empty($HTTP_X_FORWARDED_FOR)) {
  154.             $proxy_ip = $HTTP_X_FORWARDED_FOR;
  155.         } else if (!empty($HTTP_X_FORWARDED)) {
  156.             $proxy_ip = $HTTP_X_FORWARDED;
  157.         } else if (!empty($HTTP_FORWARDED_FOR)) {
  158.             $proxy_ip = $HTTP_FORWARDED_FOR;
  159.         } else if (!empty($HTTP_FORWARDED)) {
  160.             $proxy_ip = $HTTP_FORWARDED;
  161.         } else if (!empty($HTTP_VIA)) {
  162.             $proxy_ip = $HTTP_VIA;
  163.         } else if (!empty($HTTP_X_COMING_FROM)) {
  164.             $proxy_ip = $HTTP_X_COMING_FROM;
  165.         } else if (!empty($HTTP_COMING_FROM)) {
  166.             $proxy_ip = $HTTP_COMING_FROM;
  167.         } // end if... else if...
  168.  
  169.         // Returns the true IP if it has been found, else FALSE
  170.         if (empty($proxy_ip)) {
  171.             // True IP without proxy
  172.             return $direct_ip;
  173.         } else {
  174.             $is_ip = ereg('^([0-9]{1,3}\.){3,3}[0-9]{1,3}', $proxy_ip, $regs);
  175.             if ($is_ip && (count($regs) > 0)) {
  176.                 // True IP behind a proxy
  177.                 return $regs[0];
  178.             } else {
  179.                 // Can't define IP: there is a proxy but we don't have
  180.                 // information about the true IP
  181.                 return FALSE;
  182.             }
  183.         } // end if... else...
  184.     } // end of the 'PMA_getIp()' function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement