How can I get user's country flag on my site

前端 未结 5 1833
野性不改
野性不改 2021-02-10 13:04

I wanna display user\'s/visitor\'s country flag on my site.

I am using different technologies like php,jsp and simple html. So I want a code which by placing on my site,

相关标签:
5条回答
  • 2021-02-10 13:20

    My service, ipdata.co provides an IP Geolocation API on https://api.ipdata.co and serves flags on for example https://ipdata.co/flags/cu.png.

    All you have to do is know your visitors' country's iso code and you can fill it in

    ipdata.co/flags/country-code.png
    

    You can of course get the user's country code by calling https://api.ipdata.co/user-ip.

    Sample embed;

    <img src="https://ipdata.co/flags/us.png" alt="Smiley face">
    

    Gives

    Smiley face

    Edit

    We now also provide you with the country emoji flag and country emoji unicode.

    0 讨论(0)
  • 2021-02-10 13:28

    Source :

    http://www.shorter.in/#flag

    <a href="http://www.shorter.in/#flag" target="_blank"><img src="http://shorter.in/flag.php"></a>
    

    Example for the code given above.

    a busy cat http://shorter.in/flag.php

    I guess this is what you are looking for.

    0 讨论(0)
  • 2021-02-10 13:35

    You can use the GeoIP extension and then map the country in question to a given icon.

    $countryName = geoip_country_name_by_name($_SERVER['REMOTE_ADDR']);
    echo $countryName;
    

    Note that getting the country via IP is not exact.

    0 讨论(0)
  • 2021-02-10 13:37

    Yeah there is something already available and you don't have to reinvent the wheel.

    Check this thing out.

     http://api.hostip.info/flag.php?ip=12.215.42.19
    

    Grab your user's IP using PHP and pass it to the API.

    <?php
    $ip=$_SERVER['REMOTE_ADDR'];
    ?>
    

    Putting it all together

    <?php
    $ip=$_SERVER['REMOTE_ADDR'];
    echo "<img src='http://api.hostip.info/flag.php?ip=$ip' />";
    ?>
    
    0 讨论(0)
  • 2021-02-10 13:37
    1. Get the IP of visitor.

       if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
      } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
          $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
          $ip = $_SERVER['REMOTE_ADDR'];
      }
      
    2. Use ip2location to find the country of the user.

      http://dev.maxmind.com/geoip/legacy/geolite/

    3. Compare the resulting country to a list of images and display the matching image. I suggest using a database to store the country name and the path to the associated image.

    0 讨论(0)
提交回复
热议问题