Content by IP location (geo targeting)

Hello

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

Adam
awassallAsked:
Who is Participating?
 
-null-Commented:
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");
    exit(0);
    break;
    case "US":
    echo "<img src=\"my_us_banner.jpg\" />";
    exit(0);
    break;
}

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.

-null-
0
 
-null-Commented:
Hi

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

http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip

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

hth

-null-
0
 
-null-Commented:
n.b  If you're using the mysql command line client, these are the commands you want:

-null-
CREATE TABLE iptocountry (ip_from int(4), ip_to int(4), country_code2 char(2), country_code3 char(3), country_name varchar(50));
 
--AND--
 
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

0
 
awassallAuthor Commented:
Ok thanks

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

Thanks
Adam
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.