[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Content by IP location (geo targeting)

Posted on 2009-02-12
Medium Priority
Last Modified: 2013-12-13

I was wondering if you could help me. I want to target my banners on my website by the IP location of the visitor and also redirect certain pages by the IP location of the visitor. (eg visitor from UK sees a different banner than visitor from USA)

I have found a database with the neccecary data on here http://ip-to-country.webhosting.info/ but I dont know how to use this to do what I want it to.

I have good knowledge of HTML as well as basic knowledge of mysql and PHP.

Any help or advice would be much appreciated.

Many Thanks

Question by:awassall
  • 3

Expert Comment

ID: 23623367

You first need to import the csv download into mysql. The latest file download is here:


This should be the column names and types in the table you import into:

IP_FROM        NUMERICAL (DOUBLE)        Beginning of IP address range.
IP_TO       NUMERICAL (DOUBLE)       Ending of IP address range.
COUNTRY_CODE2       CHAR(2)       Two-character country code based on ISO 3166.
COUNTRY_CODE3       CHAR(3)       Three-character country code based on ISO 3166.
COUNTRY_NAME       VARCHAR(50)       Country name based on ISO 3166

In your code, this is the command you need to convert the user's IP into a long integer:

$ip_number = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR'));

You can then query the database using this sql, swapping YOUR_TABLE with your table's name:

$sql = "SELECT COUNTRY_NAME FROM YOUR_TABLE WHERE IP_FROM <=$ip_number and IP_TO >= $ip_number";



Expert Comment

ID: 23623415
n.b  If you're using the mysql command line client, these are the commands you want:

CREATE TABLE iptocountry (ip_from int(4), ip_to int(4), country_code2 char(2), country_code3 char(3), country_name varchar(50));
load data infile '/path/to/ip-to-country.csv' into table iptocountry fields terminated by ',' enclosed by '"' lines terminated by '\n';

Open in new window


Author Comment

ID: 23624477
Ok thanks

How would I then use this to show differnt content based on the country of the visitor.


Accepted Solution

-null- earned 2000 total points
ID: 23625684
When you have performed the lookup and selected the country the user is in you could do one of a number of things:

1. prefix your banners with the country code, i.e. UK_homeBanner.png,US_homeBanner.png etc  This way, you can simply prefix the country code from the lookup to the front of an image name in the IMG tag.

2. Use a switch statement in php to decide what to do:

switch($myCountryCode) {
    case "UK":
    header("Location: ukpage.htm");
    case "US":
    echo "<img src=\"my_us_banner.jpg\" />";

In this, you can create a seperate case for each country code you want to deal with, and put the code to run in the case statement, whether it be a redirect as per the example, or the banner html for that country code.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question