How does IP to Country module work

I got a request to write a brief explanation how IP to Country module work. I supposed to provide a comprehensive documentation at howforge.com but there was no time to do that. The fact is that I completely forgot until got this reminder. Now I have decided to write something useful as his request.

Database Table

ip2cc module needs 3 additional tables to make it work perfectly. You need to create all 3 tables.

  1. iso3166 is a static table to store country code in ISO3166 format for both 2 characters and 3 characters as well as country name and country number.

  2. ip2cc is another big, static table to store IP to Country database for the world. Each record will identify country code in 2-character ISO3166, network name and AS number by a range of IP address, says begin IP address and end IP address, in numeric form.

  3. ip2cc_node is the last dynamic table to record hostname of the user who editted nodes. This table is used for identifying commenters, visitors, bloggers, or anyone and then show a flag in the link area.

Finding Location from IP address

To obtain the location of given IP address, we just need table ip2cc. The detail procedure is described below.

  1. Convert 4-tuble IP address to a numeric. In PHP, we have ip2long(). Otherwise, see below PHP code for adapting.

    function ip2long($ip) {
        $ex = explode(".", $ip);
        if (count($ex)!=4) return -1;
        list($a, $b, $c, $d) = $ex;
        $a = $a*16777216;
        $b = $b*65536;
        $c = $c*256;
        return $a+$b+$c+$d;
    }
    
  2. Query a row where the given IP address is between the begin and the end.

    SELECT i.*,c.* FROM {ip2cc} i LEFT JOIN iso3166 c ON i.country_code = c.country_code2 WHERE ip_from <= %s AND ip_to >= %s
    

    where, %s is the given IP address to look up.

That's it! You get all information you want.

Developer Guide

Welcome Drupalers! Basically, ip2cc module provides a function and 3 themes.

  • ip2cc_get_country($addr) You may invoke this function to get a country profile of specified IP address in $addr. You should invode it via module_invoke() as follow.

    $country = module_invoke('ip2cc', 'get_country', $addr);
    

    $country is an object of database row so it will as below attributes.

    1. $country->ip
    2. $country->country_code2 or $country->country_code
    3. $country->country_code3
    4. $country->country_name
    5. $country->country_number
    6. $country->ip_from
    7. $country->ip_to
    8. $country->net_name
    9. $country->as_number
  • theme_ip2cc_flag($country, $addr = '') This is the most common theme provided by ip2cc. You have to invoke ip2cc_get_country() by yourself. $addr is the IP addess associate to $country. $addr is optional if you don't want to show the IP address in the title in case of unknown IP address. As a result, you will get a img tag with country code in alt, IP address and network name in title.

    th
    
  • theme_ip2cc_ip_flag($addr) You may use this theme as a shortcut to by just specifing $addr. You will get the same output of theme_ip2cc_flag() wrapped by .

    th
    
  • theme_ip2cc_ip_flag_long($addr) If you have more space you could use this theme so you will get the IP address next to the flag.

    th58.9.40.201
    

Hope it helps. :-)

Tags: , ,

developing an international postal

thank you sugree for your amazing work and the effort to make a great contribution to the Drupal open source project i have downloaded the ip2cc module and i was fallowing the installation instructions until i reached to step number 4 where is says choose data base then run fetchdb.sh i'm using windows xp and didn't understand what i'm suppose to do. i'm using a web space from a hosting provider and it has a cpanel with phpmyadmin capability's. so could you explian the fallowing step please: 4. Choose a database * IP-to-Country 1. Run fetchdb.sh to obtain ip2cc.mysql 2. Apply ip2cc.mysql * My Database 1. Download ipdb.mysql from https://howforge.com/modules/ip2cc/ipdb.mysql 2. Apply ipdb.mysql 2nd Quastion i want to know if the module have the ability to direct you to a different interface depending on your ip address. for example if you are in uk it will take you to yoursite.com/uk/ and if your in america it will take you to yoursite.com/us/ and if it does that function could you explian how can it be done. and let me know if there is a user guide for the module i'd like to read more about the how to use the module and it's functions thanks Best Regards kuwaitman

ip2cc for international postal

  1. I recommend you to use the latest version in CVS with Drupal 5 so you can skip all installation steps. The automatic install script will do everything for you.

  2. It is possible to do that using Front Page module and ip2cc. All you have to do is to ask for the country of visitor.

    $addr = $_SERVER['REMOTE_ADDR'];
    $country = module_invoke('ip2cc', 'get_country', $addr);
    drupal_goto($country->country_code);

thanks

thank you surgee for answering my post it worked well I've installed the new Drupal 5 with the new ip2cc module and also installed the front page module all went well. but sir i didn't understand the last step that you have wrote # It is possible to do that using Front Page module and ip2cc. All you have to do is to ask for the country of visitor. $addr = $_SERVER['REMOTE_ADDR']; $country = module_invoke('ip2cc', 'get_country', $addr); drupal_goto($country->country_code); my question is where do i have the code? can you explain more thanks

put it in front page

The code should be put in the front page for anonymous user. It is in front page setting page.

unable to apply the frontpage commands

Dear sugree .. thanks for all the help but i was not able to apply the commands that you gave me on my site www.mideasta.com when i go to the frontpage model and past the commands in the anonymous section in the body txt box. nothing changes. could you help me in applying the commands. i'm interested in that if you can offer me installation services for the module please contact me on my email. again thanks for the help Regards Kuwaitman

pardon me

What is your email? Anyway, you are very near the goal. I'm happy to help you as a after-sales service. You used my module so you are my customer. :-) It's my responsibility because it has been lacking of documents for so long.

please contact me at the fallowing email

please contact me at the fallowing email or at my msn messenger Regards Kuwaitman

Newbie with questions

Hi sugree, I have almost no programming skills, copy and paste is my primary method of work. So I was wondering how I could get a country flag right next to the IP in the Drupal accesslog. The search brought me your module, but I see that is only a API, so I would have to write some lines somewhere to get my wish fullfilled. Can you give me a hint on where and what to change to get som country info in the accesslog? best regards, hest

accesslog

Drupal's accesslogs are collected and displayed by statistics module in the core. I'm not recommend anyone to modify the core modules to prevent further problems for the next upgrade. The best way is to develop a new statistics module using the core module and ip2cc. Since you are not familiar to programming in PHP, I will try my best to create a module for you.

accesslog

That sounds good. I'm sure this new module can be useful to many Drupal users.

In Views

I would love to grab the "country name" and use it in views to display country specific data. Anyway you could point me in a starting direction to do that? :) Views offers a phpcode section under the filter section, but I have no idea how to use it, or how to use this module to accomplish that task... Further: Taxonomy USA UK CANADA Filter a view, by taxonomy using country name from this module. Thanks in advance!

Post new comment