Jump to content

getIP()


cyberdyne2

Recommended Posts

Hi,

I have some games on my site which include the code below for their scoring system. However, some IP addresses (eg:  82.19*.2*8.3*) are not recorded in my SQL database. The problem is that due to this, the IP banning system them doesn't function.

 

Can anyone please suggest some better code or maybe just an improvement to the code in order to record these IP's ?

 

Many thanks in advance for any help.

 

$cname = gethostbyaddr($REMOTE_ADDR);
$player_ip = getIp();
  function getIp()
  
      {
  
          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;
  
  
  
          // Get some server/environment variables values
  
          if (empty($REMOTE_ADDR)) {
  
              if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])) {
  
                  $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['REMOTE_ADDR'])) {
  
                  $REMOTE_ADDR = $_ENV['REMOTE_ADDR'];
  
              }
  
              else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['REMOTE_ADDR'])) {
  
                  $REMOTE_ADDR = $HTTP_SERVER_VARS['REMOTE_ADDR'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['REMOTE_ADDR'])) {
  
                  $REMOTE_ADDR = $HTTP_ENV_VARS['REMOTE_ADDR'];
  
              }
  
              else if (@getenv('REMOTE_ADDR')) {
  
                  $REMOTE_ADDR = getenv('REMOTE_ADDR');
  
              }
  
          } // end if
  
          if (empty($HTTP_X_FORWARDED_FOR)) {
  
              if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  
                  $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
  
                  $HTTP_X_FORWARDED_FOR = $_ENV['HTTP_X_FORWARDED_FOR'];
  
              }
  
              else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'])) {
  
                  $HTTP_X_FORWARDED_FOR = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'])) {
  
                  $HTTP_X_FORWARDED_FOR = $HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'];
  
              }
  
              else if (@getenv('HTTP_X_FORWARDED_FOR')) {
  
                  $HTTP_X_FORWARDED_FOR = getenv('HTTP_X_FORWARDED_FOR');
  
              }
  
          } // end if
  
          if (empty($HTTP_X_FORWARDED)) {
  
              if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])) {
  
                  $HTTP_X_FORWARDED = $_SERVER['HTTP_X_FORWARDED'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED'])) {
  
                  $HTTP_X_FORWARDED = $_ENV['HTTP_X_FORWARDED'];
  
              }
  
              else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED'])) {
  
                  $HTTP_X_FORWARDED = $HTTP_SERVER_VARS['HTTP_X_FORWARDED'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_FORWARDED'])) {
  
                  $HTTP_X_FORWARDED = $HTTP_ENV_VARS['HTTP_X_FORWARDED'];
  
              }
  
              else if (@getenv('HTTP_X_FORWARDED')) {
  
                  $HTTP_X_FORWARDED = getenv('HTTP_X_FORWARDED');
  
              }
  
          } // end if
  
          if (empty($HTTP_FORWARDED_FOR)) {
  
              if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  
                  $HTTP_FORWARDED_FOR = $_SERVER['HTTP_FORWARDED_FOR'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED_FOR'])) {
  
                  $HTTP_FORWARDED_FOR = $_ENV['HTTP_FORWARDED_FOR'];
  
              }
  
              else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_FORWARDED_FOR'])) {
  
                  $HTTP_FORWARDED_FOR = $HTTP_SERVER_VARS['HTTP_FORWARDED_FOR'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_FORWARDED_FOR'])) {
  
                  $HTTP_FORWARDED_FOR = $HTTP_ENV_VARS['HTTP_FORWARDED_FOR'];
  
              }
  
              else if (@getenv('HTTP_FORWARDED_FOR')) {
  
                  $HTTP_FORWARDED_FOR = getenv('HTTP_FORWARDED_FOR');
  
              }
  
          } // end if
  
          if (empty($HTTP_FORWARDED)) {
  
              if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])) {
  
                  $HTTP_FORWARDED = $_SERVER['HTTP_FORWARDED'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED'])) {
  
                  $HTTP_FORWARDED = $_ENV['HTTP_FORWARDED'];
  
              }
  
              else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_FORWARDED'])) {
  
                  $HTTP_FORWARDED = $HTTP_SERVER_VARS['HTTP_FORWARDED'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_FORWARDED'])) {
  
                  $HTTP_FORWARDED = $HTTP_ENV_VARS['HTTP_FORWARDED'];
  
              }
  
              else if (@getenv('HTTP_FORWARDED')) {
  
                  $HTTP_FORWARDED = getenv('HTTP_FORWARDED');
  
              }
  
          } // end if
  
          if (empty($HTTP_VIA)) {
  
              if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])) {
  
                  $HTTP_VIA = $_SERVER['HTTP_VIA'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['HTTP_VIA'])) {
  
                  $HTTP_VIA = $_ENV['HTTP_VIA'];
  
              }
  
              else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_VIA'])) {
  
                  $HTTP_VIA = $HTTP_SERVER_VARS['HTTP_VIA'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_VIA'])) {
  
                  $HTTP_VIA = $HTTP_ENV_VARS['HTTP_VIA'];
  
              }
  
              else if (@getenv('HTTP_VIA')) {
  
                  $HTTP_VIA = getenv('HTTP_VIA');
  
              }
  
          } // end if
  
          if (empty($HTTP_X_COMING_FROM)) {
  
              if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])) {
  
                  $HTTP_X_COMING_FROM = $_SERVER['HTTP_X_COMING_FROM'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['HTTP_X_COMING_FROM'])) {
  
                  $HTTP_X_COMING_FROM = $_ENV['HTTP_X_COMING_FROM'];
  
              }
  
              else if (!empty($HTTP_SERVER_VARS) && isset($HTTP_SERVER_VARS['HTTP_X_COMING_FROM'])) {
  
                  $HTTP_X_COMING_FROM = $HTTP_SERVER_VARS['HTTP_X_COMING_FROM'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_X_COMING_FROM'])) {
  
                  $HTTP_X_COMING_FROM = $HTTP_ENV_VARS['HTTP_X_COMING_FROM'];
  
              }
  
              else if (@getenv('HTTP_X_COMING_FROM')) {
  
                  $HTTP_X_COMING_FROM = getenv('HTTP_X_COMING_FROM');
  
              }
  
          } // end if
  
          if (empty($HTTP_COMING_FROM)) {
  
              if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])) {
  
                  $HTTP_COMING_FROM = $_SERVER['HTTP_COMING_FROM'];
  
              }
  
              else if (!empty($_ENV) && isset($_ENV['HTTP_COMING_FROM'])) {
  
                  $HTTP_COMING_FROM = $_ENV['HTTP_COMING_FROM'];
  
              }
  
              else if (!empty($HTTP_COMING_FROM) && isset($HTTP_SERVER_VARS['HTTP_COMING_FROM'])) {
  
                  $HTTP_COMING_FROM = $HTTP_SERVER_VARS['HTTP_COMING_FROM'];
  
              }
  
              else if (!empty($HTTP_ENV_VARS) && isset($HTTP_ENV_VARS['HTTP_COMING_FROM'])) {
  
                  $HTTP_COMING_FROM = $HTTP_ENV_VARS['HTTP_COMING_FROM'];
  
              }
  
              else if (@getenv('HTTP_COMING_FROM')) {
  
                  $HTTP_COMING_FROM = getenv('HTTP_COMING_FROM');
  
              }
  
          } // end if
  
  
  
          // Gets the default ip sent by the user
  
          if (!empty($REMOTE_ADDR)) {
  
              $direct_ip = $REMOTE_ADDR;
  
          }
  
  
  
          // Gets the proxy ip sent by the user
  
          $proxy_ip     = '';
  
          if (!empty($HTTP_X_FORWARDED_FOR)) {
  
              $proxy_ip = $HTTP_X_FORWARDED_FOR;
  
          } else if (!empty($HTTP_X_FORWARDED)) {
  
              $proxy_ip = $HTTP_X_FORWARDED;
  
          } else if (!empty($HTTP_FORWARDED_FOR)) {
  
              $proxy_ip = $HTTP_FORWARDED_FOR;
  
          } else if (!empty($HTTP_FORWARDED)) {
  
              $proxy_ip = $HTTP_FORWARDED;
  
          } else if (!empty($HTTP_VIA)) {
  
              $proxy_ip = $HTTP_VIA;
  
          } else if (!empty($HTTP_X_COMING_FROM)) {
  
              $proxy_ip = $HTTP_X_COMING_FROM;
  
          } else if (!empty($HTTP_COMING_FROM)) {
  
              $proxy_ip = $HTTP_COMING_FROM;
  
          } // end if... else if...
  
  
  
          // Returns the true IP if it has been found, else FALSE
  
          if (empty($proxy_ip)) {
  
              // True IP without proxy
  
              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)) {
  
                  // True IP behind a proxy
  
                  return $regs[0];
  
              } else {
  
                  // Can't define IP: there is a proxy but we don't have
  
                  // information about the true IP
  
                  return FALSE;
  
              }
  
          } // end if... else...
  
      } // end of the 'getIp()' function

Link to comment
Share on other sites

Hi.  Is your problem because your script can't detect improper IP addresses like the one above?

 

How I usually check is to explode the IP address after trimming white space with trim(), then make sure the array has 4 elements, and also check that each element is within the proper range for a IP4 ip address, and check to see that each element in that array is numeric (with something like ctype_digit()).

 

As far as your script not logging them, is $_SERVER['REMOTE_ADDR'] not giving you their IP address?  the only thing I can think of for it not logging the IP address correctly is if you have an issue with data type, or more importantly, data size for that column in your table.  could you be a little more specific as to your problem?

Link to comment
Share on other sites

Sorry, I should have added that I obscurred the IP simply to save posting what, to some, may be personal information on a public forum. The IP address was initially a full, correct IP address. Should I post the whole IP ?

 

The script detects IP addresses correctly almost always, but for some reason, this particular IP shows as blank in my SQL database, thus failing to recognise a banned IP and the score is recorded - but with a blank for the IP in the database.

 

Does this clarify the issue?

Thank you

Link to comment
Share on other sites

hmm that's weird.  you did clarify, thanks  :D

 

as far as the ip address showing up blank, i've never seen that. i've seen some weird ones like the one you intentionally masked for privacy reasons lol.  not exactly but it was sorta like that.  i would start by double checking string length vs your limit set in your table, and add trim() to your ip address to make sure it didn't come out with some hidden ascii white space character or something that's screwing things up.  i honestly don't know what to suggest since i've never encountered this. i suppose you could generate a random hash and save it into a cookie and use that to identify your users as a second means so if ip doesn't get logged you can go by the cookie's hash value instead.  not the best answer but it would work if you cant' figure out what's wrong lol

 

good luck, mate!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.