<- Back to Blog
Published: 13/11/2016

How to get a users location from an IP Address in PHP/Laravel Framework

To make an awesome user experience in today's globalised world it is important to localise your website for your visitors and users. You may have different languages or translations of your website or you simply want to show content relevant to your users location. The most reliable and simple way to do this is by getting the requested users IP Address and getting the city/country information from IP Find's API and then redirecting to the correct pages.

You can get the package on github here or install it from composer like this:

composer require tenfef/ipfind-php

use Tenfef\IPFind\IPFind;

$apiKey = "YOUR_API_KEY_HERE"; // get an API key from https://ipfind.co or pass in NULL if you plan on using < 100/day
$ipfind = new IPFind($apiKey);
$result = $ipfind->fetchIPAddress('8.8.8.8');
$country = $result->country;
{
   "ip_address":"8.8.8.8",
   "country":"United States",
   "country_code":"US",
   "continent":"North America",
   "continent_code":"NA",
   "city":"Mountain View",
   "county":"Santa Clara",
   "region":"California",
   "region_code":"CA",
   "timezone":"PST",
   "owner":"LEVEL 3 COMMUNICATIONS INC",
   "longitude":-122.0865,
   "latitude":37.3801
}