Some consumers are up in arms because that fake blog they read by someone who just happens to be from the same city they live in, turns out to be a bit phony. They’re surprised to find out that a piece of software is behind the trick, which does a GeoIP lookup on their IP address, and then uses that within the text of the splog, or ad, making the reader feel a bit more of a connection to the ad copy.
Even before these splogs popped up, I had been interested in GeoIP lookup techniques for Google Maps mashup purposes, but the splog trick finally made me figure out how it was done.
DISCLAIMER: I don’t condone the use of these techniques for deceptive advertising techniques. At best, they are disingenuous, and at worst, may be illegal, according to the FTC. Like The Force, this technique can be used for good or evil. Which path you choose is up to you.
MaxMind has a paid GeoIP City database service that costs $370 up front and $90 a month for database updates, which they advertise as 99.8% accurate in determining location from IP address. However, they also make a free database available, that is 99.5% accurate. We’ll cover the free version since the accuracy difference is negligible for our purposes.
The crucial information is spread out over four pages on the MaxMind site:
APIs for GeoIP products
GeoLite Country information
GeoLite City information
and our API of choice, the GeoIP PHP API
It should be noted that the GeoIP PHP API has three methods of being implemented, each being more efficient with webserver resources than the previous: a pure PHP module, a PECL module that allows you to embed the GeoIP C Library inside PHP, and mod_geoip, an Apache module that you can access via PHP. Since our needs are fairly minimal, we’ll go with the easiest to implement, the pure PHP module, which requires no PHP or Apache changes.
1) Download the following PHP files from MaxMind: geoipcity.inc, geoip.inc, and geoipregionvars.php .
2) Download the latest GeoLite City Binary Format file, which is updated on the 1st of each month, and gunzip it.
3) Put all of those files in their own directory somewhere on your webserver, and for this example, we’ll name the directory GeoIP.
4) Put the following code in a web-accessible PHP page, changing the PATH/TO parts to match your setup, and view the page in a web browser:
$gi = geoip_open('/PATH/TO/GeoIP/GeoLiteCity.dat', GEOIP_STANDARD);
$location = GeoIP_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
I believe you are in <?php echo $location->city ?>, <?php echo $location->region ?>
The database says I’m in Los Angeles when in fact we’re in Mission Viejo (Orange County), which isn’t a huge deal, but shows the possible slight inaccuracy in IP->location lookup.
One thing this could benefit from is a quick state abbreviation to full state name conversion function, so the text would be “Los Angeles, California” instead of “Los Angeles, CA”. I think the full spelling looks more natural in some instances. I’ve uploaded a PHP state abbreviation to state name conversion function for your use.
Update 2: I’ve uploaded my modified geoipcity.inc that includes the abbreviation to state/province function, with ON->Ontario included, and the call to that function in GeoIP_record_by_addr. Note that I could have also put the $record->region_name = Abbreviation_GetStateName($record->region); code within the _get_record function, but I chose to put it in the higher-level GeoIP_record_by_addr function.