Tuesday, December 17, 2013

GeoIp Help

Was hoping someone can point out where I am going wrong here.

 

Just testing so code is rather sloppy.

 

Related to GeoIP City / Country Location. (data is in my database, not a web call)

 

$address = $this->my_ip2long($_SERVER['REMOTE_ADDR']);

 

Calls this:

private function my_ip2long($ip){

                               

                $nums = explode('.', $ip);

                $ret = 0;

                for ($i=0; $i<4; $i++){

                $ret += intval($nums[$i]) * pow(256, 3-$i);

                }

                return $ret;

}

 

So $address =  (int) 2393010516
 
So far so good.
 
$current_address = ClassRegistry::init('IpCountry')->__findUserCountry();
Produces:
array(
                    'IpCountry' => array(
                                         'id' => '38959',
                                         'ip_from' => '2391343104',
                                         'ip_to' => '2394947583',
                                         'country2' => 'CA',
                                         'country3' => 'CAN',
                                         'country' => 'CANADA'
 
 Which is correct I am in Canada.
 
So I put that function inside __FindUserCountry(); all good so far.
 
Then

$location = ClassRegistry::init('GeoCityBlock')->find('first', array(

                                                'conditions' => array(

                                                                'and' => array(

                                                                                array(

                                                                                                'startIpNum <' => $address,

                                                                                                'endIpNum >' => $address))),

                                                                'limit' => 1,

                                                                'contain' => array(

                                                                                                      'GeoCityLocation')));
 
And presto time travel :0 I am now in Seoul Korea.
 
array(
                    'GeoCityBlock' => array(
                                         'startIpNum' => '23068672',
                                         'endIpNum' => '24117247',
                                         'locId' => '22883'
                    ),
                    'GeoCityLocation' => array(
                                         (int) 0 => array(
                                                             'locId' => '22883',
                                                             'country' => 'KR',
                                                             'region' => '11',
                                                             'city' => 'Seoul',
                                                             'postalCode' => '',
                                                             'latitude' => '37.5985',
                                                             'longitude' => '126.9783',
                                                             'metroCode' => '',
                                                             'areaCode' => ''
                                         )
                    )
 
 
Anyone have any assistance they could provide would be great.
 
Thanks,
 
Dave

 

 

No comments: