Using GeoIP in CodeIgniter application
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.




[...] our last post we explained how to use GeoIP easily in CodeIgniter. Now we are interested in getting location [...]
Get Location Information based on an address, city or country name | jigniter™
10 Jan 10 at 3:48 pm