Basically the code is something like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
function check_contry(){ $ip = $_SERVER[“REMOTE_ADDR”]; if (filter_var(@$_SERVER[‘<a href=”http://en.wikipedia.org/wiki/X-Forwarded-For”>HTTP_X_FORWARDED_FOR</a>’], FILTER_VALIDATE_IP))} $ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’]; } if (filter_var(@$_SERVER[‘HTTP_CLIENT_IP’], FILTER_VALIDATE_IP)){ $ip = $_SERVER[‘HTTP_CLIENT_IP’]; } $ipdata = @json_decode(file_get_contents(“http://www.geoplugin.net/json.gp?ip=” . $ip)); return $ipdata–>geoplugin_countryCode; } |
So we are using the GeoPlugin API to retrieve the Country code. You can also get the full country name, state, city, continent and some other stuff. Here is a simple dump of the data returned from the site:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ [“geoplugin_request”]=> string(13) “93.152.143.66” [“geoplugin_status”]=> int(206) [“geoplugin_credit”]=> string(145) “Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.” [“geoplugin_city”]=> string(6) “Sofiya” [“geoplugin_region”]=> string(11) “Grad Sofiya” [“geoplugin_areaCode”]=> string(1) “0” [“geoplugin_dmaCode”]=> string(1) “0” [“geoplugin_countryCode”]=> string(2) “BG” [“geoplugin_countryName”]=> string(8) “Bulgaria” [“geoplugin_continentCode”]=> string(2) “EU” [“geoplugin_latitude”]=> string(2) “43” [“geoplugin_longitude”]=> string(2) “25” [“geoplugin_regionCode”]=> string(0) “” [“geoplugin_regionName”]=> NULL [“geoplugin_currencyCode”]=> string(3) “BGL” [“geoplugin_currencySymbol”]=> NULL [“geoplugin_currencySymbol_UTF8”]=> string(0) “” [“geoplugin_currencyConverter”]=> int(0) } |
For more info and explanations and more complicated code check Chris Wiegman post on this topic.