For a logging system that I've been writing I came up with a way to asign
a unique id number to every IP address that visits the site.  What I do
is append every *new* IP to a new line in a file (ips.txt).  Then when I
have an IP and need the id I file() the ip log to put it in an array,
array_flip() it to make the IP the key and the index the value.  Then the
ID is dataArray[$ip] + 1.  The +1 is so there isn't an IP with the ID of
0.  When I have the id and need the IP I do the same thing except no
array_flip and subtract 1 from the ID before dataArray[$id].  This has
been working fine throughout the site in testing except in one spot. 
What I have is the IP and what I need is the ID.  This is what is done:

// Use IP address to get id
function getId ($ip) {
    $ipData = file('idData/ips.txt');
    $ipData = array_flip($ipData);

    $id = $ipData[$ip] + 1;
    return $id;
}

But for some (and only some) IPs this won't work.  So for debugging I
printed the array, the flipped array, the ip, $ipData[$ip] and $id.  The
first IP will work fine, like this:

Array ( 
  [0] => 172.170.69.74 
  [1] => 172.141.183.231 
  [2] => 172.137.79.102 
  [3] => 172.151.144.242 
  [4] => 172.150.212.129 
  [5] => 172.158.154.92 
) // correct array 

Array ( 
  [172.170.69.74 ] => 0 
  [172.141.183.231 ] => 1 
  [172.137.79.102 ] => 2 
  [172.151.144.242 ] => 3 
  [172.150.212.129 ] => 4 
  [172.158.154.92] => 5 
) // correct flipped

172.137.79.102 // correct ip

2 // correct $ipData[$ip]

3 // correct $id


But then it will go and do something like this:


Array (
  [0] => 172.170.69.74
  [1] => 172.141.183.231 
  [2] => 172.137.79.102 
  [3] => 172.151.144.242 
  [4] => 172.150.212.129 
  [5] => 172.158.154.92 
) // correct array 

Array ( 
  [172.170.69.74 ] => 0 
  [172.141.183.231 ] => 1 
  [172.137.79.102 ] => 2 
  [172.151.144.242 ] => 3 
  [172.150.212.129 ] => 4 
  [172.158.154.92] => 5 
) // correct flipped

172.151.144.242 // correct ip

// complete nothingness where $ipData[$ip] should equal 3

1 // wrong $id, should be 4

I have been trying to figure this out for three days and have come up
with nothing.  Where is the function going wrong?  
--
Kyle

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to