Pagodabox Tip : using GeoIP extension to get user’s location in Laravel

In one of the applications I’m building I needed to get user’s location (city) from their IP address.

One of the ways to go about this is to use some webservice that will convert the IP into geographical coordinates and then pass the result into your application.

Another way is to use geoip extension for PHP and get the user’s location(city) from the geoip extension. I was able to successfully use geoip extension to get user’s approximate location using free tools that now live on my Pagodabox instance.

The process of enabling GeoIP extension and using it in your Laravel application on Pagodabox is described below.

On Pagodabox list of extensions  you can see that geoip extension is included.

1) First, enable geoip in the boxfile:

    - geoip
    - curl
    - imagick
    - mbstring
    - pdo_mysql
    - mcrypt
    - gd
    - eaccelerator

2) Then, download the GeoLite database from Maxmind’s website:
Unpack it and place it into a folder within your application (for example in a folder called “GeoIP” in the root of your application )

3) Rename the database file to “GeoIPCity.dat”

4) In the Boxfile add “php_geoip_custom_directory” entry under your web1 configuration:

    - geoip
  php_geoip_custom_directory: /var/www/GeoIP

Alright, at this point your pagodabox instance will have GeoIP enabled and configured.

Now, to actually use it in your Laravel application, in a route or controller or model, use this code:

// check if geoip is loaded 
if (extension_loaded('geoip')) 
	// find user's location depending on IP and display it:
   	$geoip = geoip_record_by_name($_SERVER['HTTP_X_FORWARDED_FOR']);
   	if ($geoip) {
    	print_r($geoip, true);

At this point if you deploy your application and call this block of code you should see the output that will contain your location, city and other geo location.

Hope this is useful!

Enjoy tracking your users =)

Liked it? Take a second to support Maks Surguy on Patreon!
Become a patron at Patreon!

You may also like


  • aditya menon April 10, 2013  

    Genius! Beautiful availability of a nice feature, all for free! I’d only suggest that such applications also build in a timeout of some sort when getting the Geo data, just in case Pagodabox goes down.

  • Maks Surguy April 10, 2013  

    well the thing is that my whole application is on Pagodabox so if that goes down, my whole app will be down. The advantage of using GeoIP on pagodabox instead of another service is to minimize dependencies and contain the whole application to pagodabox instance

  • Sid April 10, 2013  

    Cool – I stopped using the extension and loaded Maxmind into a database because the default geoip binary on pagodabox was from 2010. Didn’t realise I could point it to my own folder. Thanks!

  • Maks Surguy April 10, 2013  

    Yes, that was what I needed and PagodaBox implemented that feature a while ago but it’s only found in a support ticket. Glad you can use this now!

Leave a comment