테이블 생성
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, `country` varchar(50) NOT NULL, PRIMARY KEY (`id`), KEY `geoip_lookup` USING BTREE (`ipnum_start`,`ipnum_end`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
GeoIPCountryWhois.csv
https://dev.maxmind.com/geoip/legacy/csv/
위 파일을 다운로드 한 후 todo 의 tool 에서 임포트를 한다.
CI helper 에 다음 코드를 넣은 는다.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /* * GeoIP Plugin for CodeIgniter/Blueflame - Version 1 * Writted By Paul Trippett (paul_at_techxpert_co_uk) * * 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, `country` varchar(50) NOT NULL, PRIMARY KEY (`id`), KEY `geoip_lookup` USING BTREE (`ipnum_start`,`ipnum_end`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; * * * 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: - * * $this->load->plugin('geoip'); * * geoip_update(); * geoip_lookup('xxx.xxx.xxx.xxx'); * * 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/'); * */ $ci =& get_instance(); if (!defined('GEOIP_TABLENAME')) define('GEOIP_TABLENAME', $ci->db->dbprefix.'geoip'); if (!defined('GEOIP_FILESOURCE')) define('GEOIP_FILESOURCE', 'updates/geoip/'); /** * geoip_lookup * * Looks up GeoIP data from database. * * @access public * @param string $ip The either a dotted decimal or integer or the ip address to lookup * @return string The two letter country code of the ip address. */ function geoip_lookup($ip) { // If we are passed a string ip address convert it to a Long Integer $data = (is_string($ip)) ? ip2long($ip) : $ip; $CI =& get_instance(); $query = $CI->db->query('SELECT * FROM ' . GEOIP_TABLENAME . ' WHERE ' . $data . ' BETWEEN ipnum_start AND ipnum_end'); return (string) ($query->num_rows() > 0) ? $query->row()->country_code : ''; } function geoip_country($ip) { // If we are passed a string ip address convert it to a Long Integer $data = (is_string($ip)) ? ip2long($ip) : $ip; $CI =& get_instance(); $query = $CI->db->query('SELECT * FROM ' . GEOIP_TABLENAME . ' WHERE ' . $data . ' BETWEEN ipnum_start AND ipnum_end'); return (string) ($query->num_rows() > 0) ? $query->row()->country: ''; } /** * geoip_update * * Imports GeoIP data from RIR Data * * @access public * @param null * @return null */ function geoip_update() { // Prevent script timing out set_time_limit(0); // Get instance to CodeIgniter $CI =& get_instance(); // Load Required Helpers $CI->load->helper('file'); $CI->benchmark->mark('geoip_update_start'); // Start a Transaction and clear out the old data. $CI->db->trans_start(); $CI->db->simple_query(sprintf('DELETE FROM %s;', GEOIP_TABLENAME)); // Process each file $source_files = get_filenames(GEOIP_FILESOURCE); if (empty($source_files)) show_error(sprintf('No RIR Data was found in the directory %s', GEOIP_FILESOURCE)); foreach($source_files as $file) { echo sprintf('Importing %s... ', $file); geoip_update_file(GEOIP_FILESOURCE . $file); echo 'Done.<br />'; } // Commit the changes $CI->db->trans_complete(); $CI->benchmark->mark('geoip_update_end'); echo sprintf('GeoIP update completed. Processed %d files in %d seconds', count($source_files), $CI->benchmark->elapsed_time('geoip_update_start', 'geoip_update_end')); } /** * geoip_update_file * * Imports GeoIP data from a particular text file. * */ function geoip_update_file($file) { // Get instance to CodeIgniter $CI =& get_instance(); $current_file = read_file($file); // $current_file = read_file("./updates/geoip/delegated-afrinic-latest"); //$current_file = read_file("./updates/geoip/delegated-afrinic-latest"); $query_start = sprintf('INSERT INTO %s VALUES ', GEOIP_TABLENAME); $query = ''; $count = 0; $type = ''; for ( $line = 1; $line < count($current_file); $line++ ) { $data = split('[\|]', rtrim($current_file[$line])); if (count($data) == 7) { list($registry, $cc, $type, $start, $value, $date, $status) = $data; } elseif (count($data) == 8) { list($registry, $cc, $type, $start, $value, $date, $status, $exception) = $data; } if ($type == 'ipv4') { $ip_start = $start; $ip_end = long2ip(ip2long($ip_start) + $value); $ipnum_start = ip2long($ip_start); $ipnum_end = ip2long($ip_end); if (!$count == 0) $query .= ','; $query .= sprintf("(0,'%s','%s',%d,%d,'%s')", $ip_start, $ip_end, $ipnum_start, $ipnum_end, $cc); $count++; if ($count == 50) { $CI->db->simple_query($query_start . $query); $count = 0; $query = ''; } } } // The number of records will probably not divide equally by 50 // so we execute any remainder here. if ($count > 0) { $CI->db->simple_query($query_start . $query); } }
사용 방법
$this->load->helper('geoip'); echo "<br>"; echo "접속 국가 코드 ".geoip_lookup($this->input->ip_address()); echo "<br>"; echo "<br>"; echo "접속 국가 ".geoip_country($this->input->ip_address()); echo "<br>";
geoip_update 와 geoip_update_file 함수는 안 될 것이다.
다음과 같은 방법도 있다. dat 사용
http://codeigniter-kr.org/bbs/view/source?idx=7546
댓글 ( 5)
댓글 남기기