Geotargeting, or geographical targeting, is a way for website to display content specific for a geographical location

IP Country mapping Database Products
· IP-Country Database
· IP-Country-Region
· IP-Country-Region-City
· IP-Country-ISP Database
· IP-Country-Region-City-ISP
· Target Country by IP Address
· Analyse Spider

Purchase IP Country mapping Database Purchase
Downloads Downloads
IP Country mapping Database -- Technical Details Technical Details
Contact us Contact us

IP Address Locator
IP Geolocation Articles & Tutorial (Free)

IP Country mapping Database IP Address Geotarget

What is geotargeting?

Geotargeting, or geographical targeting, is a way for website to display content specific for a geographical location. This targeted content can be advertising, latest news, or other examples (more later).

Geotargeting happens on the server, before the page is sent to the browser. When a user requests a page from the server, the server queries the user's IP address against a database of worldwide IP addresses. The database identifies the country (sometimes even city!) of the user's IP address, and then the server decides what to do.

Why is geotargeting important?

Geotargeting is important for many reasons, and now, we'll talk about two examples.

Internet marketing

Online advertising reaches everyone in the world that access a website. So if your website wants to run an advertising campaign targeted to one region only, you will need geotargeting. In this example, a certain advert will be shown only to people from certain countries, or different ad campaigns could be shown to visitors from different countries. This way, the adverts are highly targeted.


Another use of geotargeting is localization. Suppose your website is written in two languages, say English and German. By default, the English version is served. However, if a visitor comes from Germany (or any other German-speaking country), your website could send the German copy by default.

IP Address Country database: setup

The first thing you need is a database of world wide IP addresses.

  1. Easily Importing IP2Country MySQL dump file into MySQL
  2. The import process is easier by using the LOAD DATA feature available in MYSQL.
  • You download the IP database in CSV format
  • Create a new table in your database; call it ip2country
    CREATE TABLE `ip2country` (
    `ipFROM` double unsigned NOT NULL default '0',
    `ipTO` double unsigned NOT NULL default '0',
    `countryCode` char(2) NOT NULL default '',
    `countryName` varchar(64) NOT NULL default ''
    PRIMARY KEY (`ipFROM`,`ipTO`)
    ) TYPE=MyISAM;
  • You can import ip2country.csv into the mysql db.

Now that we have the IP database ready, let's use it!

Geotargeting PHP Code

The code is very simple:

  • We get the visitor's IP address...
  • ...and look it up in the database...
  • ...and figure out which country the IP address belongs to.

And the code is:

// Returns the real IP address of the user
function anp_realip()
// No IP found (will be overwritten by for
// if any IP is found behind a firewall)
$ip = FALSE;

// User is behind a proxy and check that we discard RFC1918 IP addresses
// if they are behind a proxy then only figure out which IP belongs to the
// user. Might not need any more hackin if there is a squid reverse proxy
// infront of apache.
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

// Put the IP's into an array which we shall work with shortly.
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);

for ($i = 0; $i < count($ips); $i++) {
// Skip RFC 1918 IP's, and
// below.
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
$ip = $ips[$i];
// Return with the found IP or the remote address
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);

// Function to convert IP address ( to IP number (0 to 256^4-1)
function anp_ip2long ($IPaddr)
if ($IPaddr == "") {
return 0;
} else {
$ips = split ("\.", "$IPaddr");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0] *

// Replace this MYSQL server variables with actual configuration
$mysql_server = "";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";

$mysql_dbname = "ip2";
$mysql_table = "ip2country";

// Retrieve the real IP address of the user
$ipaddress = anp_realip();

// Convert IP address to IP number for querying database
$ipno = anp_ip2long($ipaddress);

// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass)
or die("Could not connect to MySQL database");

// Connect to the IP2Country database
mysql_select_db("$mysql_dbname") or die("Could not select database");

// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT countryCode , countryName FROM $mysql_table WHERE ipTo =$ipno AND ipFrom<=$ipno";

// Execute SQL query
$result = mysql_query($query) or die("IP2Country Query Failed");

// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);

// Keep IP specific information into different variables
$countryCode = $row-countryCode;
$countryName = $row-countryName;

// Free recordset and close database connection

Copyright ©2001-2017 Jgsoft Associates. All rights reserved.