NYCPHP Meetup

NYPHP.org

[nycphp-talk] Best practice for comparing IP addresses

Sicular, Alexander as867 at columbia.edu
Sun Jun 1 09:07:40 EDT 2003


Phil,

I gather there are many ways to accomplish this... This is just one that
may work for you...


>From within your for loop...

if(intval($yourIPArray[0].$yourIPArray[1].$yourIPArray[2])==intval($ipAr
ray[0].$ipArray[1].$ipArray[2])){
Do stuff
}

The explode should take your ip address and make it an array with 4
items in it. I think the items will be text. Using the '.' to
concatenate the first three values and intval() to turn them into a
number you should be able to see if they match. 

Of course a regex maybe more robust...

Good luck,
alex

-----Original Message-----
From: Phil Powell [mailto:soazine at erols.com] 
Sent: Sunday, June 01, 2003 2:19 AM
To: NYPHP Talk
Subject: [nycphp-talk] Best practice for comparing IP addresses


Consider the following lines of code:

if ($hasEnteredNick == 1 && file_exists("$pathStart/banned_ips.xml")) {
    $fileID = fopen("$pathStart/banned_ips.xml") or die("Cannot open
file $pathStart/banned_ips.xml");
    $stuff = fread($fileID, filesize("$pathStart/banned_ips.xml"));
fclose($fileID);
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $stuff, $bannedIPArray, $extraStuff);
    xml_parser_free($parser);
    $yourIPArray = explode(".", $REMOTE_ADDR);
    for ($i = 0; $i < sizeOf($bannedIPArray); $i++) {
     $ipArray = explode($bannedIPArray[$i][attributes][IP], ".");

     if (($bannedIPArray[$i][attributes][NICK] === $nick ||
         implode(".", array_pop($yourIPArray)) == implode(".",
array_pop($ipArray))
         ) &&
         $bannedIPArray[$i][attributes][NICK] !== "admin"
        ) {
      // NICKNAME FOUND IN banned_ips.xml FILE - THEY HAVE BEEN BANNED
FROM CHATROOM
      $hasEnteredNick = 0;
      if ($bannedIPArray[$i][attributes][NICK] === $nick) {
       $errorMsg = "You are using a banned nickname, please select
another";
      } else {
       $errorMsg = "The user at this machine ($REMOTE_ADDR) has been
banned from the chatroom";
      }
      break;
     }
    }
         
   }

What I want to do is compare an IP address from a parsed XML file with
the user's IP address.  If a match is found only in the first three
numbers (xx.xx.xx.*) then action is taken.  I thought about using the
following algorithm to do so, but does anyone else have a better
suggestion?  I am not accustomed to either implode, explode or
array_pop, so I am not sure if they are being used properly w/o
extensive testing.

Thanx
Phil



--- Unsubscribe at http://nyphp.org/list/ ---



-----------------------------------
This transmission is confidential and
intended solely for the person or
organization to whom it is addressed.
It may contain privileged and
confidential information.  If you are
not the intended recipient, you
should not copy, distribute or take
any action in reliance on it.

If you believe you received this
transmission in error, please
notify the sender.
--------------------------------





More information about the talk mailing list