We help IT Professionals succeed at work.

Content by IP location (geo targeting)

awassall asked
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

Watch Question


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";



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


Ok thanks

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

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.


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.