GeoIP Plugin for CodeIgniter
Written By Paul Trippett (paul_at_softlab-bss_com)
To use this plugin you must create a table in your database with the following schema: -
CREATE TABLE `geoip` ( `id` int(10) unsigned NOT NULL auto_increment, `ip_start` varchar(15) NOT NULL, `ip_end` varchar(15) NOT NULL, `ipnum_start` float NOT NULL, `ipnum_end` float NOT NULL, `country_code` char(2) NOT NULL, PRIMARY KEY (`id`), KEY `geoip_lookup` USING BTREE (`ipnum_start`,`ipnum_end`) ) ENGINE=InnoDB;
Place this file in your application/plugins/ directory and download the following files in to the {ci_root}/updates/geoip/ directory.
ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest ftp://ftp.arin.net/pub/stats/arin/delegated-arin-latest ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest ftp://ftp.apnic.net/pub/stats/iana/delegated-iana-latest
You should then be able to use the following code in your controller: -
Lookups:
$this->load->plugin('geoip');
geoip_update();Country Lookups:
$this->load->plugin('geoip');
geoip_lookup('xxx.xxx.xxx.xxx');NOTE: geoip_update() needs only to be called once after you update the files from the RIR FTP sites.
Should you wish to place the files in a different directory or use a different table name you can put the following code in your
/application/config/config.php file: -
define('GEOIP_TABLENAME', 'geoip');
define('GEOIP_FILESOURCE', 'updates/geoip/');