Wednesday, December 18, 2013

Re: GeoIp Help

That's not how to do an 'and' in CakePHP conditions.

Conditions in CakePHP will use 'and' by default, for items in the same array, unless explicitly keyed by an 'or'.

This will get you closer to your goal, though you might want to check the greater than / less than operations for equality, and IPs that are at the beginning or end of the range.

 'conditions' => array(

    'startIpNum <=' => $address,

    'endIpNum >=' => $address),

'limit' => 1,

Regards
Reuben Helms

On Wednesday, 18 December 2013 05:22:22 UTC+10, advantage+ wrote:

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

 

 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: