Convert IP Address to Location in PHP Php Tricks by Rajesh Kumar Sahanee - April 30, 2017September 11, 20170 Post Views: 8,372 Hello Friends, Today I am going to share how to Convert IP Address to Location in PHP. Actually recently I required to find Country Code from an IP Address in one of my project, and I found the solution on Internet and I thought to share with you. This code basically work on web service provided by http://www.geoplugin.net and web service url is http://www.geoplugin.net/json.gp Using this web service we can not only get Country code but also Country name, State, City, Address, Continent, etc. This web service also provides GPS longitude and latitude but this is not implement in the given ip_info function but you can implement if you need by having idea from ip_info function. This web service takes IP address as parameter and gives json as response. We can call web service like http://www.geoplugin.net/json.gp?ip=XXX.XXX.XXX.XXX(we can use both IPv4 and IPv6 ip addresses). So Here is the code ip_info PHP <?php function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) { $output = NULL; if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) { $ip = $_SERVER["REMOTE_ADDR"]; if ($deep_detect) { if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_CLIENT_IP']; } } $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose))); $support = array("country", "countrycode", "state", "region", "city", "location", "address"); $continents = array( "AF" => "Africa", "AN" => "Antarctica", "AS" => "Asia", "EU" => "Europe", "OC" => "Australia (Oceania)", "NA" => "North America", "SA" => "South America" ); if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) { $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip)); if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) { switch ($purpose) { case "location": $output = array( "city" => @$ipdat->geoplugin_city, "state" => @$ipdat->geoplugin_regionName, "country" => @$ipdat->geoplugin_countryName, "country_code" => @$ipdat->geoplugin_countryCode, "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)], "continent_code" => @$ipdat->geoplugin_continentCode ); break; case "address": $address = array($ipdat->geoplugin_countryName); if (@strlen($ipdat->geoplugin_regionName) >= 1) $address[] = $ipdat->geoplugin_regionName; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); break; case "city": $output = @$ipdat->geoplugin_city; break; case "state": $output = @$ipdat->geoplugin_regionName; break; case "region": $output = @$ipdat->geoplugin_regionName; break; case "country": $output = @$ipdat->geoplugin_countryName; break; case "countrycode": $output = @$ipdat->geoplugin_countryCode; break; } } } return $output; } //USAGE OF FUNCTION echo ip_info("160.238.70.23", "Country") . "<br/>"; // India echo ip_info("160.238.70.23", "Country Code") . "<br/>"; // IN echo ip_info("160.238.70.23", "State") . "<br/>"; // Haryāna echo ip_info("160.238.70.23", "City") . "<br/>"; // Bhaskola echo ip_info("160.238.70.23", "Address") . "<br/>"; // Bhaskola, Haryāna, India print_r(ip_info("160.238.70.23", "Location")); // Array ( [city] => [state] => [country] => India [country_code] => IN [continent] => Asia [continent_code] => AS ) 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 <?phpfunction ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) { $output = NULL; if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) { $ip = $_SERVER["REMOTE_ADDR"]; if ($deep_detect) { if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_CLIENT_IP']; } } $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose))); $support = array("country", "countrycode", "state", "region", "city", "location", "address"); $continents = array( "AF" => "Africa", "AN" => "Antarctica", "AS" => "Asia", "EU" => "Europe", "OC" => "Australia (Oceania)", "NA" => "North America", "SA" => "South America" ); if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) { $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip)); if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) { switch ($purpose) { case "location": $output = array( "city" => @$ipdat->geoplugin_city, "state" => @$ipdat->geoplugin_regionName, "country" => @$ipdat->geoplugin_countryName, "country_code" => @$ipdat->geoplugin_countryCode, "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)], "continent_code" => @$ipdat->geoplugin_continentCode ); break; case "address": $address = array($ipdat->geoplugin_countryName); if (@strlen($ipdat->geoplugin_regionName) >= 1) $address[] = $ipdat->geoplugin_regionName; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); break; case "city": $output = @$ipdat->geoplugin_city; break; case "state": $output = @$ipdat->geoplugin_regionName; break; case "region": $output = @$ipdat->geoplugin_regionName; break; case "country": $output = @$ipdat->geoplugin_countryName; break; case "countrycode": $output = @$ipdat->geoplugin_countryCode; break; } } } return $output;} //USAGE OF FUNCTIONecho ip_info("160.238.70.23", "Country") . "<br/>"; // Indiaecho ip_info("160.238.70.23", "Country Code") . "<br/>"; // INecho ip_info("160.238.70.23", "State") . "<br/>"; // Haryānaecho ip_info("160.238.70.23", "City") . "<br/>"; // Bhaskolaecho ip_info("160.238.70.23", "Address") . "<br/>"; // Bhaskola, Haryāna, India print_r(ip_info("160.238.70.23", "Location")); // Array ( [city] => [state] => [country] => India [country_code] => IN [continent] => Asia [continent_code] => AS ) Output Referrence https://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip Thanks