3

I'm trying to use the free MaxMind GeoLite2 code to be able to determine the country of a specific IP address.

I am using the Composer-free method that was posted here: Get a localized name of the users city via Maxmind GeoLite2 Free

I'm sure its incredibly simple, but I can't figure out how to actually pass an IP address and have it return the country.

After the $reader = new Reader... line I have $place = $reader->country('##.###.##.###'); (where the #'s are actual IP address numbers) and it's not working. I tried replacing 'country' with 'city' and that didn't work either. I'm sure its something simple, I'm just not sure what parameters I need to be using to get the country returned.

The error that is shown in the error log is 'PHP Fatal error: Call to undefined method MaxMind\Db\Reader::city() in <<< path to benchmark.php >>>)'

Any ideas/suggestions would be greatly appreciated.

Community
  • 1
  • 1
Mark
  • 739
  • 1
  • 9
  • 15

2 Answers2

6

There's no city() or country() functions defined in the files you're including (based on the answer you linked to.) Instead you're supposed to use get() to get the IP geographic information, like so:

require_once __DIR__ . '/' . 'Db/Reader.php';
require_once __DIR__ . '/' . 'Db/Reader/Decoder.php';
require_once __DIR__ . '/' . 'Db/Reader/InvalidDatabaseException.php';
require_once __DIR__ . '/' . 'Db/Reader/Metadata.php';
require_once __DIR__ . '/' . 'Db/Reader/Util.php';     // new 2014/09
use MaxMind\Db\Reader;
$mmdb= 'GeoLite2-Country.mmdb';
$reader = new Reader( __DIR__  . '/' . $mmdb );
$ipData = $reader->get('##.###.##.###');
echo $ipData['country']['names']['en'];

Where you replace ##.###.##.### with the IP you want to get info for. Obviously this requires you have all the required code files and GeoLite2-Country.mmdb

So the complete steps would be:

  1. Download the MaxMind-DB-Reader-php from https://github.com/maxmind/MaxMind-DB-Reader-php
  2. Copy the Db folder found in src/MaxMind to the directory with the file containing the above code.
  3. Download the GeoLite2 Country MaxMind DB from http://dev.maxmind.com/geoip/geoip2/geolite2/
  4. Unzip the downloaded gzip and copy the GeoLite2-Country.mmdb file to the same directory as the file containing the above code.
  5. You should now be able to run the above code! Just make sure you replace ##.###.##.### with a real IP.
AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
  • Wow, thank you sooo much! I knew it had to be something relatively simple, and had tried something similar but had been missing the 'names' and 'en' parameters. Thanks again, my headache is now gone!! :) If you happen to know any tricks that will make it process a bit quicker, I'd love to hear them. – Mark Oct 07 '14 at 20:58
  • @Mark It runs quite quick (0.0017 seconds to get, 0.0047 seconds if you include loading the DB) for me. Is it slow for you? – AlliterativeAlice Oct 07 '14 at 20:59
  • the first couple times I ran it, it took about 5 seconds for the page to load....but after seeing your message I've tried it again multiple times and in different browsers and it has been quite quick all of those times. Might have just been a temporary issue with my own internet, as I'm happy with the speed its going at now! ...I'm using this as part of a link redirect (user clicks a link, and then gets redirected to a localized version of that site based on their location, so that's why at first I was concerned it seemed to be a bit slow. Thanks again for taking the time! – Mark Oct 07 '14 at 21:06
0

This is a simple way. First you have to insert the user IP in MySQL. Then you have to run fetch query, something like this

//database connect or includ database php file

//user_ip detect

$geo = json_decode(file_get_contents("http://extreme-ip-lookup.com/json/$user_ip")); 
$country = $geo->country; 
$city = $geo->city; 
$ipType = $geo->ipType; 
$businessName = $geo->businessName; 
$businessWebsite = $geo->businessWebsite; 
echo "Location of $user_ip: $city, $country\n"; 
echo $ip_address;
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Sammrafi
  • 399
  • 4
  • 8