IP geolocation is something that’s needed more and more as people publish & trade internationally. Stores want to offer different pricing, currencies, shipping rules to different countries, or provide content in different languages.
Many systems have something already baked in, but working on a project recently I needed to do some simple country-level IP geolocation. My previous iteration had used a third party service, but that slowed things down, and had a tendency to be unreliable – recently going AWOL for a couple of weeks!
There are some free GeoLocation databases provided by MaxMind, but I doubt I’m telling you anything new there, it seems to be fairly well known about. However I’ve never actually used them myself directly, so in I dived.
Firstly I used their public PHP wrapper:
I was able to pull it into my project using Composer really easily, download the database and hook it up simply. Running a geolocation really was as simple as their documentation suggested, I ended up with this in my code:
$reader = new GeoIp2\Database\Reader( GEOIP_DB_PATH ); $record = $reader->country( $this->get_user_ip() ); $country = $record->country->isoCode;
Really nice and simple, and a lot more reliable than my previous solution, and definitely something I’d use again in the future without hesitation.
Image credit: https://commons.wikimedia.org/wiki/File:Red1234.jpg
- Stuff I’ve used
- Error tracking with Sentry
- Autotrack for Google Analytics
- WordPress performance tracking with Time-stack
- Enforce user password strength
- WYSIWYG with Summernote
- Backing up your Laravel app
- Adding Google Maps to your Laravel application
- Activity logging in Laravel
- Image handling in PHP with Intervention Image
- Testing Laravel emails with MailThief
- Assessing software health
- IP Geolocation with MaxMind’s GeoLite2
- Uptime monitoring with Uptime Robot
- Product tours with Hopscotch
- Background processing for WordPress
- Using oEmbed resources in Laravel