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
Asked
Active
Viewed 2,884 times
2
-
let me get this straight, what are the options in the dropdown? IPs? – Joseph Feb 17 '12 at 09:53
-
related: http://stackoverflow.com/questions/4422817/how-create-geoip-functionality-in-php-project – Kaii Feb 17 '12 at 09:53
-
Welcome to SO. Please take time to read our [FAQ](http://stackoverflow.com/faq) and write clear posts. Thus helping others help you. – Starx Feb 17 '12 at 09:54
-
possible duplicate of http://stackoverflow.com/questions/5741347/how-to-get-country-code-and-currency-code-by-ip-address – Gordon Feb 17 '12 at 10:53
4 Answers
2
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>

Kaii
- 20,122
- 3
- 38
- 60
0
MaxMind GeoIP has a free API for PHP for finding someone's country based on their IP.

Sticky
- 1,022
- 1
- 11
- 19
0
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/

StathisG
- 1,159
- 1
- 10
- 18