GeoIP not returning current location in custom module

7 months 1 week ago #74151

Justin Griffin's Avatar Justin Griffin

Following other posts on this topic, I'm attempting to create a custom module to return the current user country. I've included this in the default.php template file to first echo to the screen and verify I am getting the location:

require_once JPATH_LIBRARIES . '/geoip/geoip.php';
$geo = new GeoIp($ip);
$location = $geo->get();
echo $location->country;

Do you see where I am going wrong here?


7 months 1 week ago #74152

Peter van Westen's Avatar Peter van Westen Admin

That code should work... if you have defined the $ip variable. You can remove that to simply use the current IP:

require_once JPATH_LIBRARIES . '/geoip/geoip.php';
$geo = new GeoIp();
$location = $geo->get(array('country'));
echo isset($location->country) ? $location->country : 'Country not found';
This will not work on your local host, as that will use IP, which is not connected to a location.