Jump to content


Photo

Change function to superglobal


  • Please log in to reply
No replies to this topic

#1 Baldo

Baldo
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 03 July 2006 - 07:29 PM

I want to change the code from a ip-function to superglobal.

Is this a right change or did I do something wrong ???

<?
function ip() {
  // global $REMOTE_ADDR;
  // global $HTTP_X_FORWARDED_FOR, $HTTP_X_FORWARDED, $HTTP_FORWARDED_FOR, $HTTP_FORWARDED;
  // global $HTTP_VIA, $HTTP_X_COMING_FROM, $HTTP_COMING_FROM;
  // global $HTTP_SERVER_VARS, $HTTP_ENV_VARS;
   
  if (empty($_SERVER['REMOTE_ADDR'])) {
  // if (empty($REMOTE_ADDR)) {
    if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) { 
      $_SERVER['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; 
      // $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
    }
    else if (!empty($_ENV) && isset($_ENV['REMOTE_ADDR'])) {
      $_SERVER['REMOTE_ADDR'] = $_ENV['REMOTE_ADDR'];
      // $REMOTE_ADDR = $_ENV['REMOTE_ADDR'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) {
    // else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['REMOTE_ADDR'])) {
      $_SERVER['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
      // $REMOTE_ADDR = $HTTP_SERVER_VARS['REMOTE_ADDR'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['REMOTE_ADDR'])) {
      $_SERVER['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
      // $REMOTE_ADDR = $HTTP_ENV_VARS['REMOTE_ADDR'];
    }
    else if ($_SERVER('REMOTE_ADDR')) {
    // else if (@getenv('REMOTE_ADDR')) {
      $_SERVER['REMOTE_ADDR'] = $_SERVER('REMOTE_ADDR');
      // $REMOTE_ADDR = getenv('REMOTE_ADDR');
    }
  }
  
  if (empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  // if (empty($HTTP_X_FORWARDED_FOR)) {
    if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      $_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
      // $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
      $_SERVER['HTTP_X_FORWARDED_FOR'] = $_ENV['HTTP_X_FORWARDED_FOR'];
      // $HTTP_X_FORWARDED_FOR = $_ENV['HTTP_X_FORWARDED_FOR'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'])) {
      $_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
      // $HTTP_X_FORWARDED_FOR = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'])) {
      $_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
      // $HTTP_X_FORWARDED_FOR = $HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'];
    }
    else if ($_SERVER('HTTP_X_FORWARDED_FOR')) {
    // else if (@getenv('HTTP_X_FORWARDED_FOR')) {
      $_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER('HTTP_X_FORWARDED_FOR');
      // $HTTP_X_FORWARDED_FOR = getenv('HTTP_X_FORWARDED_FOR');
    }
  } // end if
  
  if (empty($_SERVER['HTTP_X_FORWARDED'])) {
  // if (empty($HTTP_X_FORWARDED)) {
    if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
      $_SERVER['HTTP_X_FORWARDED'] = $_SERVER['HTTP_X_FORWARDED'];
      // $HTTP_X_FORWARDED = $_SERVER['HTTP_X_FORWARDED'];
    }
    else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED'])) {
      $_SERVER['HTTP_X_FORWARDED'] = $_ENV['HTTP_X_FORWARDED'];
      // $HTTP_X_FORWARDED = $_ENV['HTTP_X_FORWARDED'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
    // else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED'])) {
      $_SERVER['HTTP_X_FORWARDED'] = $_SERVER['HTTP_X_FORWARDED'];
      // $HTTP_X_FORWARDED = $HTTP_SERVER_VARS['HTTP_X_FORWARDED'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_FORWARDED'])) {
      $_SERVER['HTTP_X_FORWARDED'] = $_SERVER['HTTP_X_FORWARDED'];
      // $HTTP_X_FORWARDED = $HTTP_ENV_VARS['HTTP_X_FORWARDED'];
    }
    else if ($_SERVER('HTTP_X_FORWARDED')) {
    // else if (@getenv('HTTP_X_FORWARDED')) {
      $_SERVER['HTTP_X_FORWARDED'] = $_SERVER('HTTP_X_FORWARDED');
      // $HTTP_X_FORWARDED = getenv('HTTP_X_FORWARDED');
    }
  }
  if (empty($_SERVER['HTTP_FORWARDED_FOR'])) {
  // if (empty($HTTP_FORWARDED_FOR)) {
    if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
      $_SERVER['HTTP_FORWARDED_FOR'] = $_SERVER['HTTP_FORWARDED_FOR'];
      // $HTTP_FORWARDED_FOR = $_SERVER['HTTP_FORWARDED_FOR'];
    }
    else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED_FOR'])) {
      $_SERVER['HTTP_FORWARDED_FOR'] = $_ENV['HTTP_FORWARDED_FOR'];
      // $HTTP_FORWARDED_FOR = $_ENV['HTTP_FORWARDED_FOR'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
    // else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_FORWARDED_FOR'])) {
      $_SERVER['HTTP_FORWARDED_FOR'] = $_SERVER['HTTP_FORWARDED_FOR'];
      // $HTTP_FORWARDED_FOR = $HTTP_SERVER_VARS['HTTP_FORWARDED_FOR'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_FORWARDED_FOR'])) {
      $_SERVER['HTTP_FORWARDED_FOR'] = $_SERVER['HTTP_FORWARDED_FOR'];
      // $HTTP_FORWARDED_FOR = $HTTP_ENV_VARS['HTTP_FORWARDED_FOR'];
    }
    else if ($_SERVER('HTTP_FORWARDED_FOR')) {
    // else if (@getenv('HTTP_FORWARDED_FOR')) {
      $_SERVER['HTTP_FORWARDED_FOR'] = $_SERVER('HTTP_FORWARDED_FOR');
      // $HTTP_FORWARDED_FOR = getenv('HTTP_FORWARDED_FOR');
    }
  }
  
  if (empty($_SERVER['HTTP_FORWARDED'])) {
  // if (empty($HTTP_FORWARDED)) {
    if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
      $_SERVER['HTTP_FORWARDED'] = $_SERVER['HTTP_FORWARDED'];
      // $HTTP_FORWARDED = $_SERVER['HTTP_FORWARDED'];
    }
    else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED'])) {
      $_SERVER['HTTP_FORWARDED'] = $_ENV['HTTP_FORWARDED'];
      // $HTTP_FORWARDED = $_ENV['HTTP_FORWARDED'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
    // else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_FORWARDED'])) {
      $_SERVER['HTTP_FORWARDED'] = $_SERVER['HTTP_FORWARDED'];
      // $HTTP_FORWARDED = $HTTP_SERVER_VARS['HTTP_FORWARDED'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_FORWARDED'])) {
      $_SERVER['HTTP_FORWARDED'] = $_SERVER['HTTP_FORWARDED'];
      // $HTTP_FORWARDED = $HTTP_ENV_VARS['HTTP_FORWARDED'];
    }
    else if ($_SERVER('HTTP_FORWARDED')) {
    // else if (@getenv('HTTP_FORWARDED')) {
      $_SERVER['HTTP_FORWARDED'] = $_SERVER('HTTP_FORWARDED');
      // $HTTP_FORWARDED = getenv('HTTP_FORWARDED');
    }
  }
  
  if (empty($_SERVER['HTTP_VIA'])) {
  // if (empty($HTTP_VIA)) {
    if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
      $_SERVER['HTTP_VIA'] = $_SERVER['HTTP_VIA'];
      // $HTTP_VIA = $_SERVER['HTTP_VIA'];
    }
    else if (!empty($_ENV) && isset($_ENV['HTTP_VIA'])) {
      $_SERVER['HTTP_VIA'] = $_ENV['HTTP_VIA'];
      // $HTTP_VIA = $_ENV['HTTP_VIA'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
    // else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_VIA'])) {
      $_SERVER['HTTP_VIA'] = $_SERVER['HTTP_VIA'];
      // $HTTP_VIA = $HTTP_SERVER_VARS['HTTP_VIA'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_VIA'])) {
      $_SERVER['HTTP_VIA'] = $_SERVER['HTTP_VIA'];
      // $HTTP_VIA = $HTTP_ENV_VARS['HTTP_VIA'];
    }
    else if ($_SERVER('HTTP_VIA')) {
    // else if (@getenv('HTTP_VIA')) {
      $_SERVER['HTTP_VIA'] = $_SERVER('HTTP_VIA');
      // $HTTP_VIA = getenv('HTTP_VIA');
    }
  }
  if (empty($_SERVER['HTTP_X_COMING_FROM'])) {
  // if (empty($HTTP_X_COMING_FROM)) {
    if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
      $_SERVER['HTTP_X_COMING_FROM'] = $_SERVER['HTTP_X_COMING_FROM'];
      // $HTTP_X_COMING_FROM = $_SERVER['HTTP_X_COMING_FROM'];
    }
    else if (!empty($_ENV) && isset($_ENV['HTTP_X_COMING_FROM'])) {
      $_SERVER['HTTP_X_COMING_FROM'] = $_ENV['HTTP_X_COMING_FROM'];
      // $HTTP_X_COMING_FROM = $_ENV['HTTP_X_COMING_FROM'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
    // else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_COMING_FROM'])) {
      $_SERVER['HTTP_X_COMING_FROM'] = $_SERVER['HTTP_X_COMING_FROM'];
      // $HTTP_X_COMING_FROM = $HTTP_SERVER_VARS['HTTP_X_COMING_FROM'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_COMING_FROM'])) {
      $_SERVER['HTTP_X_COMING_FROM'] = $_SERVER['HTTP_X_COMING_FROM'];
      // $HTTP_X_COMING_FROM = $HTTP_ENV_VARS['HTTP_X_COMING_FROM'];
    }
    else if ($_SERVER('HTTP_X_COMING_FROM')) {
    // else if (@getenv('HTTP_X_COMING_FROM')) {
      $_SERVER['HTTP_X_COMING_FROM'] = $_SERVER('HTTP_X_COMING_FROM');
      // $HTTP_X_COMING_FROM = getenv('HTTP_X_COMING_FROM');
    }
  }
  
  if (empty($_SERVER['HTTP_COMING_FROM'])) {
  // if (empty($HTTP_COMING_FROM)) {
    if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])) {
      $_SERVER['HTTP_COMING_FROM'] = $_SERVER['HTTP_COMING_FROM'];
      // $HTTP_COMING_FROM = $_SERVER['HTTP_COMING_FROM'];
    }
    else if (!empty($_ENV) && isset($_ENV['HTTP_COMING_FROM'])) {
      $_SERVER['HTTP_COMING_FROM'] = $_ENV['HTTP_COMING_FROM'];
      // $HTTP_COMING_FROM = $_ENV['HTTP_COMING_FROM'];
    }
    else if (!empty($_SERVER['HTTP_COMING_FROM']) && isset($_SERVER['HTTP_COMING_FROM'])) {
    // else if (!empty($HTTP_COMING_FROM) && isset($HTTP_SERVER_VARS['HTTP_COMING_FROM'])) {
      $_SERVER['HTTP_COMING_FROM'] = $_SERVER['HTTP_COMING_FROM'];
      // $HTTP_COMING_FROM = $HTTP_SERVER_VARS['HTTP_COMING_FROM'];
    }
    else if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])) {
    // else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_COMING_FROM'])) {
      $_SERVER['HTTP_COMING_FROM'] = $_SERVER['HTTP_COMING_FROM'];
      // $HTTP_COMING_FROM = $HTTP_ENV_VARS['HTTP_COMING_FROM'];
    }
    else if ($_SERVER('HTTP_COMING_FROM')) {
    // else if (@getenv('HTTP_COMING_FROM')) {
      $_SERVER['HTTP_COMING_FROM'] = $_SERVER('HTTP_COMING_FROM');
      // $HTTP_COMING_FROM = getenv('HTTP_COMING_FROM');
    }
  }
  
  if (!empty($_SERVER['REMOTE_ADDR'])) {
  // if (!empty($REMOTE_ADDR)) {
    $direct_ip = $_SERVER['REMOTE_ADDR'];
    // $direct_ip = $REMOTE_ADDR;
  }
  
  $proxy_ip = '';
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  // if (!empty($HTTP_X_FORWARDED_FOR)) {
    $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    // $proxy_ip = $HTTP_X_FORWARDED_FOR;
  }
  else if (!empty($_SERVER['HTTP_X_FORWARDED'])) {
  // else if (!empty($HTTP_X_FORWARDED)) {
    $proxy_ip = $_SERVER['HTTP_X_FORWARDED'];
    // $proxy_ip = $HTTP_X_FORWARDED;
  }
  else if (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
  // else if (!empty($HTTP_FORWARDED_FOR)) {
    $proxy_ip = $_SERVER['HTTP_FORWARDED_FOR'];
    // $proxy_ip = $HTTP_FORWARDED_FOR;
  }
  else if (!empty($_SERVER['HTTP_FORWARDED'])) {
  // else if (!empty($HTTP_FORWARDED)) {
    $proxy_ip = $_SERVER['HTTP_FORWARDED'];
    // $proxy_ip = $HTTP_FORWARDED;
  }
  else if (!empty($_SERVER['HTTP_VIA'])) {
  // else if (!empty($HTTP_VIA)) {
    $proxy_ip = $_SERVER['HTTP_VIA'];
    // $proxy_ip = $HTTP_VIA;
  }
  else if (!empty($_SERVER['HTTP_X_COMING_FROM'])) {
  // else if (!empty($HTTP_X_COMING_FROM)) {
    $proxy_ip = $_SERVER['HTTP_X_COMING_FROM'];
    // $proxy_ip = $HTTP_X_COMING_FROM;
  }
  else if (!empty($_SERVER['HTTP_COMING_FROM'])) {
  // else if (!empty($HTTP_COMING_FROM)) {
    $proxy_ip = $_SERVER['HTTP_COMING_FROM'];
    // $proxy_ip = $HTTP_COMING_FROM;
  }
   
  if (empty($proxy_ip)) {
    return $direct_ip;
  }
  else {
    $is_ip = ereg('^([0-9]{1,3}\.){3,3}[0-9]{1,3}', $proxy_ip, $regs);
    if ($is_ip && (count($regs) > 0)) {
        return $regs[0];
    }
    else {
      return '0.0.0.0';
    }
  }
}
?>

Please help me with this.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users