jigniter™

jQuery & CodeIgniter – Perfect combination for web application

Archive for the ‘geoip’ tag

Get Location Information based on an address, city or country name

without comments

In our last post we explained how to use GeoIP easily in CodeIgniter. Now we are interested in getting location information from a particular address using a Geocoder tool. For that reason, we’d recommend to take a look over How to use Google Maps API with PHP from Zimuel’s blog.

Zimuel exposes the following scenario:

Scenario: you have to check the validity of the geo data of a list of customers stored into a database. Using the Google Maps API you can easly compare the data stored into your database with the google data and fix the errors.

Moreover, his solution using Google Maps API proposes the following solution.

A PHP class that uses the Google Maps API to provide geographic information from a generic address (city, country, street+city, country. etc.).

So, based on that basis we would be interested in integrating this solution into CodeIgniter application. In order to do that, we can achieve the following steps:

1. Downloading the GMaps library from Zimuel’s blog.

2. Unpacking the zip and copy the GMaps.php file into your library directory: /application/libraries/GMaps.php

3. Put the content under example.php into your controller class where you are interested in using the Google Maps library. For example, you can use the following code to get information from a location assuming that $gmaps_key is your Google Maps key (get it from here for free), and $search is the variable containing the search string (ie: an address, a city, etc.:


        $this->load->library('GMaps', $gmaps_key );
        if ($this->gmaps->getInfoLocation( $search )) {

            echo 'Address: '.$this->gmaps->getAddress().'';
            echo 'Country name: '.$this->gmaps->getCountryName().'';
            echo 'Country name code: '.$this->gmaps->getCountryNameCode().'';
            echo 'Administrative area name: '.$this->gmaps->getAdministrativeAreaName().'';
            echo 'Postal code: '.$this->gmaps->getPostalCode().'';
            echo 'Latitude: '.$this->gmaps->getLatitude().'';
            echo 'Longitude: '.$this->gmaps->getLongitude().'';

            $this->data['lat'] = $this->gmaps->getLatitude();
            $this->data['lng'] = $this->gmaps->getLongitude();

        } else {
            echo "The response of Google Maps is empty";
        }

Finally, the text displayed in the page contains geo information such as latitude, longitude, postal code, country code and name, etc.

Written byadmin

January 10th, 2010 at 3:48 pm

Posted inSnippets Ideas

Tagged with ,

Using GeoIP in CodeIgniter application

with one comment

The company called Maxmind published free resources to use Geo localized IP services in your web applications. I remember this service is there from long time ago.

Now, we are interested in getting geo information from users in a website, so it could be a good idea to integrate GeoIP services as a CodeIgniter plugin.

@Burak posted a short but nice article at phpandstuff.com explaining how to integrate GeoIP in a PHP script.

So, based on that article, I’d integrate the GeoIP as a plugin, by following these steps:

1. Create a new plugin file under application/plugins/geo_ip.php and I put there the content that is inside geoip.inc

2. We’d need to copy the GeoIP.dat file to any location inside our application. I used resources/geo/GeoIP.dat as location.

3. Then we’d need to load the plugin file, we can do that on any particular controller file (on demand) or we can add it as autoloaded plugin in application/config/autoload.php

4. I went to the controller class where I want to use GeoIP to grab the user’s country name and code, and put this snippet:


        $gi = geoip_open('resources/geo/GeoIP.dat',GEOIP_STANDARD);
        $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
        $this->data['country_code'] = $country_code;
        $this->data['country_name'] = $country_name;
        $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
        // close the database
        geoip_close($gi);

That’s all, assuming that finally you will have the Country Code and Country Name under $this->data variable. You can see more info about why I use this way to pass information to view, under Using a Super Controller Class in CodeIgniter.

Written byadmin

January 9th, 2010 at 9:07 am