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