Fetching the current country name using ip address in php [closed]

淺唱寂寞╮ 提交于 2019-12-24 02:18:42

问题


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


回答1:


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>



回答2:


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




回答3:


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:


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



来源:https://stackoverflow.com/questions/9326061/fetching-the-current-country-name-using-ip-address-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!