Experts only: Show image based on country ip

Discussion in 'Programming' started by Zoti Media Group, Feb 27, 2018.

  1. #1
    Hello Everyone.

    After searching for days without success, am asking the experts of DP to help me on this:

    I want to show image based on country ip. Lets say there is a user with german IP and he shuuld see berlin.jpg. The one with china ip should show image china.jpg.

    I want to put it as background like: <img src="$function_here" />

    I hope someone can help me. Thank you very much!
     
    Zoti Media Group, Feb 27, 2018 IP
  2. Zoti Media Group

    Zoti Media Group Notable Member

    Messages:
    1,599
    Likes Received:
    113
    Best Answers:
    2
    Trophy Points:
    265
    Digital Goods:
    2
    #2
    Ok I have found this but wont work as it should be:

    <script>
    $.get("http://freegeoip.net/json/", function(response) {
        $("#ip").html("IP: " + response.ip);
        $("#country_code").html(response.country_code);
    
        var country = response.country_code;
        var europe = ['AL', 'AD', 'AT', 'BY', 'BE', 'BA', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FO', 'FI', 'FR', 'DE', 'GI', 'GR', 'HU', 'IS', 'IE', 'IT', 'LV', 'LI', 'LT', 'LU', 'MK', 'MT', 'MD', 'MC', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SM', 'RS', 'SK', 'SI', 'ES', 'SE', 'CH', 'UA', 'VA', 'RS', 'IM', 'RS', 'ME'];
        var inEU = europe.indexOf(country) !== -1;
    
        if (country == 'US' || country == 'CA') document.getElementById(country).style.display = "block";
        else if (inEU)
            document.getElementById('Europe').style.display = "block";
        else
            document.getElementById('World').style.display = "block";
    }, "jsonp");
    </script>
    Code (markup):
    #US { text-align: left; color: blue; display:none;}
    #CA { text-align: left; color: blue; display:none;}
    #World { text-align: left; color: blue; display:none;}
    #Europe { text-align: left; color: blue; display:none;}
    #AT { text-align: left; color: blue; display:none;} 
    Code (CSS):

    When I type this: <div id="country_code"></div> it wont show the content of #AT (my country in this case)
     
    Zoti Media Group, Feb 27, 2018 IP
  3. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #3
    Actually, it doesn't take to be an expert to make such a function, anyone with basic PHP skills could do that in minutes. Here are the steps:

    1. Get details of specified IP address. I see you use freegeoip.net, and it's fine.
    2. "Extract" country code (variable is named country_code if you use the service mentioned above) from freegeoip.net's response. Something like:
    $response_array=some function you use to connect to freegeoip.net;
    $country_code=$response_array['country_code'];
    PHP:
    3. Store country-specific images somewhere on your server in country codes format, like images/US.jpg, images/LT.jpg and so on.
    4. Output the correct image using this code:
    <img src="images/<?php echo $country_code; ?>.jpg">
    PHP:
     
    phpmillion, Feb 27, 2018 IP
    sarahk likes this.
  4. Zoti Media Group

    Zoti Media Group Notable Member

    Messages:
    1,599
    Likes Received:
    113
    Best Answers:
    2
    Trophy Points:
    265
    Digital Goods:
    2
    #4

    Thank you for the reply.

    Should I replace only the first two lines and the rest is ok?
     
    Zoti Media Group, Feb 27, 2018 IP
  5. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #5
    No, you should write your basic PHP code. I guess you want to use PHP for that because you posted it in PHP forum or am I wrong? The code you posted uses JS, I don't think that code is complete anyways.
     
    phpmillion, Feb 28, 2018 IP