Follow Us!

How to Get a Users Geo Location?

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

  • http://www.claudiudesign.org/ Web Design Romania

    This is interesting. Nice tutorial. Thx

  • Joe

    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!

  • http://www.absolute-cbt.co.uk/distance-learning-why.asp Toni

    Great post – we will be using MaxMind shortly.

  • http://www.settings.biz/settings-Desktop.asp Charlie

    Very informative tutorial – you have a great site here.

  • http://www.prosaptools.com/products.html SAP Tools

    Excellent site – very interesting artilces.

  • http://www.generic-specialist.com generic

    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?

  • http://www.pspgoguide.com/ Buy PSP Go

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

  • http://lovegraphics.org/ Love Graphics

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

  • http://www.oes-tsetnoc.org oes tsetnoc

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

  • http://www.emailbreaks.com may

    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

  • http://www.mxstore.com.au/index.php/apparel-clothing.html motocross clothing

    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.

  • http://www.mxstore.com.au/index.php/apparel-clothing.html motocross clothing

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

  • http://www.sherayapi.com söve

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

  • Maddy

    Hi Author

    Thx a lot for such a nice tutorial,I canni tell ya how long I have been lookin’ for this kinda tutorial…

    ta…

  • http://www.colourgraphicdesign.com miamiwebdesign

    hey John, thanks for this information, I was looking for a code like this since I would like to offer advertisement based on user’s geo-location, I just haven’t found the right script until now. Thanks