Geo-Targeting PHP Script
Friday August 15, 2008 // Share on Facebook
I spent a few minutes with Dr_Ngo and Neil Turner at Affiliate Summit, and we were talking about geo-targeting users. Pretty powerful stuff, if your landing page can say, “Find singles in Buffalo, NY” or something similar (whatever your offer is about).
The easy (but ghetto) way to do this to put the city & state in the query string. So it’d be like page.php?city=Buffalo&state=NY. Then in your code, you just echo the city and state variables, like < ?= htmlentities(stripslashes($_GET['city'])) ? >. But what if you just want 1 landing page, without using the query string? It’s actually pretty simple, and I’m going to walk you though it.
So first, you need a database. Here’s a good free one. So from your ssh line, you’ll want to get this database by typing:
wget http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Cron this to run monthly (looks like they do updates on the 1st, so run your cron on the 5th or so) and then unzip it:
gunzip GeoLiteCity.dat.gz
OK, now you’re going to want to get the PHP module to use this data. From this directory, download
- geoip.inc
- geoipcity.inc
- geoipregionvars.php
So in a single directory, you’ll have these 3 files and GeoLiteCity.dat. Now you’re about to see how unbelievably easy this script is:
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']);
echo(”<pre>”);
print_r($rsGeoData);
echo(”</pre>”);geoip_close($gi);
You’ll see I just do a print_r to show all the variables available to you, but for me, I see:
geoiprecord Object
(
[country_code] => US
[country_code3] => USA
[country_name] => United States
[region] => NY
[city] => Buffalo
[postal_code] => 14217
[latitude] => 42.9761
[longitude] => -78.8727
[area_code] => 716
[dma_code] => 514
)
So now your landing page will say, “Find singles in < ?= htmlentities($rsGeoData->city . “, ” . $rsGeoData->region) ? >”.
With the latitude & longitude, you can even show a map of the area using the Google Maps API. Having the Zip Code would be great for zip submit offers (only 2 1 left in 14217! - Enter your Zip Code to claim the prize!) or lots of other great ideas using a Zip Code database.
Beware of foreign IPs, though. If you’re not in the US, I don’t know what city or region will give back. You can replace $_SERVER['REMOTE_ADDR'] above with a known IP to see what it returns.
In case you were wondering, I started this installing this script at 3:40 and had it done at 3:50. It took me longer to write this blog post than it did to install the script, so don’t be afraid to try it yourself. If you have problems, though, just contact me.
This entry was posted on Friday, August 15th, 2008 at 11:47 am and is filed under Affiliate Marketing. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Ken Savage said...
You must have been thinking the same things I was last weekend. I’m doing the same thing the ghetto way now through my ppc stuff but I’m going to take you up on this script and try that out.
I am looking to show ads on organic results based on coutry IP later on too but this is a good start. Nice stuff Eric.
August 16th, 2008
Neil said...
Nice! Thanks for script. The zip submit idea is great.
August 20th, 2008
Dave said...
Hey Eric good explanation … I’ve always wondered how to do this. Thanks!
August 29th, 2008
Kevin Carlson said...
Just FYI, some hosting providers have disabled the fopen() function used by geoip_open, for security reasons.
To make it work, the geoip.inc file would need to be rewritten to use the CURL library instead of fopen().
I’ve alerted Maxmind to this issue, hopefully they will update their API to address this…
September 28th, 2008
BC$ MobileTV - Press » Blog Archive » Geo-Targeting with ZipCodeMap said...
[...] can build an excellent, enterprise-grade Geo-Targeting solution by combining a GeoIP script with the zip/area code database available from [...]
October 5th, 2008