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.

    <img src="/sites/howforge.com/modules/ip2cc/flags/th.png"  width="14" height="14" alt="th" title="th Thailand 58.9.40.201 for Truehisp" />
    
  • 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 <span></span>.

    <span><img src="/sites/howforge.com/modules/ip2cc/flags/th.png"  width="14" height="14" alt="th" title="th Thailand 58.9.40.201 for Truehisp" /></span>
    
  • 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.

    <span><img src="/sites/howforge.com/modules/ip2cc/flags/th.png"  width="14" height="14" alt="th" title="th Thailand 58.9.40.201 for Truehisp" />58.9.40.201</span>
    

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 http://www.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
kuwaitman@gmail.com
or at my msn messenger
the_twisted@hotmail.com

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!

Video Converter for Mac is

Video Converter for Mac is the most powerful Video Converter for Mac OS software which allows you to convert videos between popular video formats and audio formats. The easy-to-use Video Converter for Mac lets you to enjoy your videos on iPod, PSP, Mobile Phone, Zune, iPhone, Apple TV and MP4/MP3 player. This Video Converter for Mac allows you to set the destination, the name of output files.iPod Converter for Mac is the best Video to iPod Converter for Mac OS X software for you. With the iPod Converter for Mac you can easily convert popular video formats including FLV, MPEG1, MPEG2, DivX, Xvid, WMV, 3GP, 3G2, MOV, AVI, MPEG TS/TP (for HD Video), etc to iPod compatible MPEG-4 videos. And also it is not only and iPod Converter for Mac, but it can convert all popular videos to iPhone, Apple TV, PSP, PS3, Youtube (web), Creative Zen, iRiver PMP, Archos, MP4, various mobile phones and many other digital video and audio players.MP4 Converter for Mac is an excellent Mac MP4 converter which can not only convert MP4 to MP3, WMA, AAC, WAV, etc but also convert avi, flv, wmv, mpeg, divx etc to MP4 on Mac os x to enjoy your favourite video with MP4 Converter for Mac os. Compared with other mp4 converter Mac software, the MP4 Converter for Mac provides powerful edit functions, such as setting the output settings, cropping and trimming files, merging several files into one, etc. With this MP4 Converter for Mac, you can easily convert mp4 videos to varoius format for Mac.

How to download YouTube

How to download YouTube videos for Mac and convert the .FLV files on Mac OS? Here are the easy-to-use and top-user-rating YouTube Video Grabber Mac and Mac FLV converter tools. It is safe to install and no spyware or adware guarenteed.
With the YouTube Video Grabber Mac application, you can easily download youtube for mac and convert YouTube videos on your Mac OS. It can automatically detect the YouTube video files from the YouTube web pages you have opened and download multiple files once a time. YouTube Video Grabber Mac is a totally free download youtube videos for mac, because if you don’t want to convert downloaded videos to your mobile players, you don’t have to pay for it.
FLV Converter Mac is a powerful FLV Converter for Mac software which can convert FLV video files to all popular video formats and convert FLV to popular audio formats for Mac OS X users. FLV Converter Mac OS X softwrare supports all video popular formats indcluding FLV, MPG, MPEG1, MPEG2, MP4, 3GP, 3G2, MOV, AVI, Mpeg TS/TP (for HD Video), etc. The Mac FLV Converter can also convert FLV to MP3, WAV, OGG, 3GP, AAC and M4A, AC3, APE, etc audio.

Post new comment

The content of this field is kept private and will not be shown publicly.