Check out our newest Tutorial project. Tutorial Grad the site that submits your tutorials for you.

Subscribe to our rss feed

How to Get a Users Geo Location?

Posted in PHP Tutorials, SEO Tutorials, Web Development Tutorials by John Ward on the February 22nd, 2009

Every wonder how all of those dating ads know that you are looking for hot girls near (insert your city here)? Well if you are a complete nerd like me you probably ignored the hot girls and wondered: “How did they know where I live?”. There are a few simple ways to do this. Today I am going to show you how to GeoCode using MaxMind’s free version.

MaxMind offers a paid version of this script that probably offers much more functionality. For what I need though, the free database does the job. I checked the results via a few proxies and they matched the US city fine. I am not sure how this works with international visitors, but I use it for US targeted traffic (It does pick up Canadian cities and Provinces fine….eh)..

First off you are going to need to download the Database and PHP API code:

MaxMind GeoLiteCity
Geoip.inc
Geoipcity.php
Geoipregionvars.php

Once you download all of the files. Extract them all and upload them to the same folder on your server.

Now to use the API we need to include a few files. Then we are going to open the database and check the users location:

include("geoip.inc");
include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("./GeoLiteCity.dat", GEOIP_STANDARD);
$rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

This will grab the information, but what can we do with it. To find out what variables you can use print out the array:

include("geoip.inc");
include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("./GeoLiteCity.dat", GEOIP_STANDARD);
$rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

print "<pre>";
print_r($rsGeoData);
print "</pre>";

The results should output something like this:

geoiprecord Object
(
    [country_code] => US
    [country_code3] => USA
    [country_name] => United States
    [region] => TN
    [city] => Memphis
    [postal_code] =>
    [latitude] => 35.1242
    [longitude] => -89.9521
    [area_code] => 901
    [dma_code] => 640
)

I ran the above code using a Memphis based proxy. As you can see MaxMinds database gives us:
Country Abbreviation in two formats,
Full country name,
Region (state/province),
City,
Postal code (this usually works but is blank in the example),
Latitude,
Longitude,
Area Code,
DMA code (Designated Market Area as in Nielsen Media tv/radio market areas in the US)

Ok so now that we know what everything is, all we have to do is echo the results in the proper places. For example:

echo $rsGeoData->city;
echo ' ,';
echo $rsGeoData->region;

Would out put: Memphis, TN.
So if you were going to use this in a template you would use something like this in the header:

<?php
include("geoip.inc");
include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("./GeoLiteCity.dat", GEOIP_STANDARD);
$rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

$location =  $rsGeoData->city.','.$rsGeoData->region;

?>

Then in your template wherever you want the City, State to display simple:


<h1>Meet girls near <?php echo $location;?></h1>

The your site might look something like this:
php geo coding

Popularity: unranked [?]

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit

Related Posts

Tutorial Grad - Recent Tutorials

13 Responses to 'How to Get a Users Geo Location?'

Subscribe to comments with RSS or TrackBack to 'How to Get a Users Geo Location?'.


  1. on March 3rd, 2009 at 7:14 am

    This is interesting. Nice tutorial. Thx

  2. Joe said,

    on March 31st, 2009 at 12:22 am

    Hi John,

    Great tutorial! Thanks!

    This is just what I have been looking for. One question: Any idea how I could just place the required files in one place and call them with an include file from different folders instead of having to have them live in the same folder that I am calling them from?

    Thanks again!

  3. Toni said,

    on April 5th, 2009 at 7:15 am

    Great post – we will be using MaxMind shortly.

  4. Charlie said,

    on May 6th, 2009 at 7:44 am

    Very informative tutorial – you have a great site here.

  5. SAP Tools said,

    on May 7th, 2009 at 4:13 pm

    Excellent site – very interesting artilces.

  6. generic said,

    on August 26th, 2009 at 7:37 pm

    Fast question: Any idea how I could just place the required files in one place and call them with an include file from different folders instead of having to have them live in the same folder that I am calling them from?

  7. Buy PSP Go said,

    on September 2nd, 2009 at 3:38 am

    If you want to cheat you can use this service: http://api.hostip.info/country.php


  8. on September 5th, 2009 at 1:23 pm

    I have seen these ads and wondered how they knew the exact city where I was from. Pretty interesting stuff.

  9. oes tsetnoc said,

    on September 23rd, 2009 at 6:27 am

    I need to learn coding to implement the code. right now, i only have basic knowledge in php api code

  10. may said,

    on November 7th, 2009 at 10:04 am

    i also have seen some geo location ads, and now i know that coder are using this geo location to track the location of one computer


  11. on January 9th, 2010 at 8:23 am

    Have tried the Google documentation and I cant figure out how to geocode multiple points, then take these points and get the center and correct zoom level. I do admit that I am a bit of a newb but any examples would be awesome! Save my poor brain from exploding! Thanks.


  12. on January 9th, 2010 at 8:25 am

    If by “geocode” you mean global coordinates latitude/longitude,then Google Earth works fine. Type in the address and the coordinates are displayed.

  13. söve said,

    on March 2nd, 2010 at 3:25 am

    I need to learn coding to implement the code.Thanks.

Leave a Comment