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&#91;'REMOTE_ADDR'&#93;);
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

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join the TeamTutorials mailing list and get the latest tips, tricks, and special discounts for members only.

Thanks! Please click the link in the email we just sent you to confirm.

  • 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!

  • Great post – we will be using MaxMind shortly.

  • Very informative tutorial – you have a great site here.

  • Excellent site – very interesting artilces.

  • 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?

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

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

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

  • 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

  • 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.

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

  • 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…

  • 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

  • guest

    I’m not getting city and region loction .

  • your files are missing, please fix this article.

  • Neil

    I tried as tutorial explains but I get empty page. Does this work for Canadian cities? Any idea what I could have done wrong?