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.