How To Get Visitor’s Country by IP in PHP

This example shows how to get a visitor’s country using their ip in PHP language. This script doesn’t show how to get ip which we will discuss in another article.

<?php
// PHP code to obtain country, city,
// continent, etc using IP Address

$ip = '209.205.218.43';

// Use JSON encoded string and converts
// it into a PHP variable
$ipdat = @json_decode(file_get_contents(
	"http://www.geoplugin.net/json.gp?ip=" . $ip));

echo 'Country Name: ' . $ipdat->geoplugin_countryName . "</br>";
echo 'City Name: ' . $ipdat->geoplugin_city . "</br>";
echo 'Continent Name: ' . $ipdat->geoplugin_continentName . "</br>";
echo 'Latitude: ' . $ipdat->geoplugin_latitude . "</br>";
echo 'Longitude: ' . $ipdat->geoplugin_longitude . "</br>";
echo 'Currency Symbol: ' . $ipdat->geoplugin_currencySymbol . "</br>";
echo 'Currency Code: ' . $ipdat->geoplugin_currencyCode . "</br>";
echo 'Timezone: ' . $ipdat->geoplugin_timezone;

?>

Output in browser:




Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] certain criteria a different page. You can lookout our guides on how to get user ip in php or how to get country name, currency etc in php here. If you want to redirect the users from old page to a new page on a permanent basis then also […]