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.
-
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.
-
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.
-
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.
-
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; }
-
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 viamodule_invoke()
as follow.$country = module_invoke('ip2cc', 'get_country', $addr);
$country
is an object of database row so it will as below attributes.$country->ip
-
$country->country_code2
or$country->country_code
$country->country_code3
$country->country_name
$country->country_number
$country->ip_from
$country->ip_to
$country->net_name
$country->as_number
-
theme_ip2cc_flag($country, $addr = '')
This is the most common theme provided by ip2cc. You have to invokeip2cc_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 inalt
, IP address and network name intitle
. -
theme_ip2cc_ip_flag($addr)
You may use this theme as a shortcut to by just specifing$addr
. You will get the same output oftheme_ip2cc_flag()
wrapped by.
-
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.58.9.40.201
Hope it helps. :-)
Tags: drupal, geolocation, ip2cc
- sugree's blog
- 3898 reads
developing an international postal
ip2cc for international postal
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.
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.
thanks
put it in front page
unable to apply the frontpage commands
pardon me
please contact me at the fallowing email
Newbie with questions
accesslog
accesslog
In Views
Post new comment