Content by IP location (geo targeting)

Posted on 2009-02-12
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 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
    LVL 5

    Expert Comment


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


    LVL 5

    Expert Comment

    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

    Ok thanks

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

    LVL 5

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    PHP Radio Array 4 27
    Sort order issue from query/json string in DB to xml 5 25
    PHP and SOAP Woes 7 22
    CSRF session variables 23 10
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
    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…
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now