Html Code to show Different Banner based on Country of visitor

Discussion in 'HTML & Website Design' started by alai, Sep 7, 2014.

  1. #1
    Does anyone know of an easy way to show a different Image/Banner based on what Country (IP address) the visitor is from??

    What I want to do is:
    If visitor is from US show Image 1
    Elseif visitor is from Canada show Image 2
    Elseif.............
    Else show image 99

    Also I am using PHP, HTML or Java code is what I would like to use.
     
    alai, Sep 7, 2014 IP
  2. COBOLdinosaur

    COBOLdinosaur Active Member

    Messages:
    515
    Likes Received:
    123
    Best Answers:
    11
    Trophy Points:
    95
    #2
    You will probably need to use a geolocation database. If you Google "geolocation database" you will get a range of free and paid options.
     
    COBOLdinosaur, Sep 7, 2014 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,812
    Likes Received:
    4,535
    Best Answers:
    123
    Trophy Points:
    665
    #3
    php will make the job really easy.

    Pick your geolocation provider - some are "faster" than others - pick one that makes an api call so that you don't have to maintain your own geolocation database. From the results you'll be able to get a short country code. Then on your script all you have to do is

    <img src="/images/banner<?php echo $countryCode; ?>.png">
    Code (markup):
    and it will automatically go and find the right banner. In my case it would look for bannerNZ, @COBOLdinosaur would get bannerCA and you would get bannerDK

    fwiw this is what I use on one of my sites for an enquiry email sent internally
    
    $ip = $_SERVER['REMOTE_ADDR'];
    $host = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key={$myAPIKey}&ip={$ip}");
    $host=explode(';',$host);
    
    $content .= "<tr>
       <td valign='top'>IP Address</td>
       <td>{$ip}<br/>{$host[5]}, {$host[3]}</td>
    </tr>";
    Code (markup):
     
    Last edited: Sep 7, 2014
    sarahk, Sep 7, 2014 IP
  4. alai

    alai Member

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    Thank you very musch
     
    alai, Sep 7, 2014 IP