Fetching the current country name using ip address

2019-07-13 02:32发布

I have a dropdown in which i fetch country name from database and i want to select current country name based on there ip address in dropdown box

4条回答
forever°为你锁心
2楼-- · 2019-07-13 02:54

MaxMind GeoIP has a free API for PHP for finding someone's country based on their IP.

查看更多
叼着烟拽天下
3楼-- · 2019-07-13 02:57

You'll have to use an IP Geolocation web service. Most of them are paid, but they provide some (usually limited) free access as well.

I have used http://www.maxmind.com/ and http://ipinfodb.com/ successfully in the past and a friend has good things to say about http://www.geoplugin.com/

查看更多
我只想做你的唯一
4楼-- · 2019-07-13 03:13

system('traceroute ' . $trace_ip_addr); // Trace IP address.

查看更多
别忘想泡老子
5楼-- · 2019-07-13 03:15

Use PHP GeoIP API. NOTE: you need to setup the Maxmind GeoIP API database before you can use the functions.

<select name="securityqustion"  class="securityqustion" id="security_qustion">
<?php
  // will resolve 2-character ISO country code
  $request_country = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);  

  $countries = array("DE" => "Germany", "FR" => "France", ...);  // define list
  foreach ($countries as $country_code => $country_label) {
    if ($request_country == $country_code) 
      $selected = "selected"
    else 
      $selected = "";
    echo "<option value=\"{$country_code}\" {$selected}>{$country_label}</option>\n";
  }
?>
</select>
查看更多
登录 后发表回答