Use GeoIP and PHP to calculate users location
South Coast Web Design Ltd
Use GeoIP to find user location with PHP Jul 20

Good afternoon,

Just recently whilst working on our new website : Car Catcher UK I had to create an area for website users to enter their postcode.

Obviously for the search option to work to its fullest we made this a required field and also added a distance dropdown.

but that meant when we land on that page we would have no way of getting the initial results easily.

So i went looking around for ideas. At first i decided to set a default location of Central London and get the longtitude and lattitude of that.

This was fine but i wanted to go a little bit further.

I could have used HTML5 geolocation but to be honest the popup window asking to share my location really started to bug me.

So I started looking elsewhere. I did find a nice little script by MAXMIND called GeoLite which allowed you to download a full file which converted IP addresses to a GEO Location in JSON format.

There were a few tutorials out on how to implement this system and it meant downloading the class etc..

I then stumbled across a free GEOIP api based on the MAXMIND GeoLite system.

This api can be found at : http://www.telize.com/

This again returns the result extremely quickly in JSON format.

All the examples on the site show you how to use Javscript and Jquery to retreive the results.

Which was ok if I was then going to AJAX the results into a SESSION and then reload the page.

However i did nt want to do this I wanted just load the page get the location on inititial page load and go from there.

So i had a choice use FILE_GET_CONTENTS or CURL.

I decided to use CURL.

At first it kept giving me the location of London at our datacentre. This was because the page loading was from the server address.

So I figured you must assign the users IP Address to the GeoIP URL.

Heres my final code :

<!--?php 
SESSION_START();
// Grab users IP address (basic method)
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $url = "http://www.telize.com/geoip/".$ipaddress;
    $ch = curl_init();
// Disable SSL verification
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
    curl_setopt($ch, CURLOPT_URL,$url);
// Execute
    $result=curl_exec($ch);
// Closing
    curl_close($ch);

// get the results and json_decode the answer
    $resulting = json_decode($result, true);
// sometimes telize will not give you a postcode from ip so default to London
    $_SESSION['postcode'] = ($resulting['postal_code'] != '' ? $resulting['postal_code'] : '-');
// if did not get any lattitude or longitude from ip lookup default also to London
    $_SESSION['userLng'] = ($resulting['longitude'] != '' ? $resulting['longitude'] : 0.1420);
    $_SESSION['userLat'] = ($resulting['latitude'] != '' ? $resulting['latitude'] : 51.5010);
&lt;/pre&gt;
&lt;p&gt;I start a php session so I can store the Location details to save looking it again on every page refresh.&lt;br ?-->
As you can see I setup the API url and append the IP Address.

I initialise the curl request and request the JSON data from Telize.

This data returns in an array and I simply use the postal_code, longitude and latitude variables.

There are lots of other variables returned as well and by doing a print_r($resulting) will show other available variables.

Sample list of what is returned:

{
"timezone":"Europe\/London",
"isp":"Virgin Media Limited",
"region_code":"K5",
"country":"United Kingdom",
"dma_code":"0",
"area_code":"0",
"region":"Poole",
"ip":"86.18.40.179",
"asn":"AS5089",
"continent_code":"EU",
"city":"Poole",
"postal_code":"BH15",
"longitude":-1.9753,
"latitude":50.7324,
"country_code":"GB",
"country_code3":"GBR"
}

In my form request after the initial page load a postcode will always be entered so I only use this on initial page load.
I also set a browser cookie with a 14 day expire and store the 3 variables within this.
When the page is initialised it firstly checks for a Cookie and uses that, if its not stored it then does the curl lookup.

Once i have the longitude and lattitude I use this with a MySql command to find distance.
To view a full working version on this have a look at http://www.carcatcher.co.uk and it is used on the main search box and also on the dealer directory pages.
I hope this gives you a easy way to find an approx location without HTML5 and only using PHP.

Leave a Response


Top