Skip to content
Advertisement

geoplugin php giving country and ip of hosting server

I’m using geoplugin php to get the country and ip of visitors on my site, but its returning the ip and country of hosting server instead of user who visit the site, I’m using Hostinger as my hosting.

$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp")); 
$country = $ipdat->geoplugin_countryName; 
$ip = $ipdat->geoplugin_request;

its working fine on xampp localhost.

Advertisement

Answer

Your code behavior is an expected behavior since the GEO plugin site gets the IP address from where the script request came and gives details accordingly. If you want user/visitor’s data, then you will need to make use of user’s remote IP stored in $_SERVER global variable and pass this IP to GEO plugin to get the details as mentioned in their docs.

Snippet:

<?php

$data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));

$country = $data['geoplugin_countryName'];
$ip = $data['geoplugin_request'];

echo $country," ",$ip,"<br/>";

print_r($data);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement