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

2020-06-04 14:35发布

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, visitors can see and it should run in all platform.

In short I want country detection API. If anybody can help me, I'll be very thankful.

标签: php html jsp
5条回答
相关推荐>>
2楼-- · 2020-06-04 15:07

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' />";
?>
查看更多
▲ chillily
3楼-- · 2020-06-04 15:12

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.

查看更多
混吃等死
4楼-- · 2020-06-04 15:14

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.

查看更多
等我变得足够好
5楼-- · 2020-06-04 15:15

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.

查看更多
beautiful°
6楼-- · 2020-06-04 15:19
  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.

查看更多
登录 后发表回答