One of the most common things that you'll do in affiliate marketing is redirect people based on their country. Whether you're making use of a traffic source where visitors come from every which where, or whether you're making sure Facebook ad approvers don't freak out, you'll use this code over and over again.
So, here's a quick plug-and-play script to get everything working straight away.
NOTE - if you're using the Stack That Money mobile tracker (which works for Web too), you don't need to do this - redirection is built in.
Setup
First of all, you'll need to follow these instructions to install the new Maxmind GeoIP database. That means that your IP-based location detection will be as accurate as possible.
Creating The Script
Next, create a new PHP file on the server you've installed the GeoIP database on - call it whatever you like.
Copy and paste this code into it:
<?PHP
require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';
$ipaddress = $_SERVER["REMOTE_ADDR"];
use \GeoIp2\Database\Reader;
$reader = new Reader('$_SERVER['DOCUMENT_ROOT']./geolocation/GeoLite2-City.mmdb');
$record = $reader->omni($ipaddress);
$subid = $_GET['subid'];
switch($record->country->isoCode) {
case 'GEOCODE':
$redirect = 'OFFERURL'.$subid;
header("Location:$redirect");
exit;
case 'US':
$redirect = 'http://f5media.com/aff_offer.php?aff_id=325423&s1=sampleoffer&s2='.$subid;
header("Location:$redirect");
exit;
default:
$redirect = 'OFFERURL'.$subid;
header("Location:$redirect");
exit;
}
?>
<?PHP
require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';
$ipaddress = $_SERVER["REMOTE_ADDR"];
use \GeoIp2\Database\Reader;
$reader = new Reader('$_SERVER['DOCUMENT_ROOT']./geolocation/GeoLite2-City.mmdb');
$record = $reader->omni($ipaddress);
$subid = $_GET['subid'];
switch($record->country->isoCode) {
case 'GB':
$redirect = 'http://f5media.com/aff_offer.php?aff_id=325423&s1=sampleoffer&s2='.$subid;
header("Location:$redirect");
exit;
default:
$redirect = 'http://www.google.com'.$subid;
header("Location:$redirect");
exit;
}
?>
http://www.yourserver.com/REDIRECT_PAGE.php?subid=[[subid]]
http://www.yourserver.com/REDIRECT_PAGE.php?subid=
Thank you very much Caurmen, this is awesome.
I'll try this right now.
Also, in this thread:
http://stmforum.com/forum/showthread...our-Own-Server
You wrote:
@julien - Thanks!
I haven't had time to look into those tutorials yet - there's some original research to do, and I've got some non-tech stuff I need to catch up with on STM (look for exciting improvements soon!), so it'll be a couple of weeks - but they are coming.
Oh, and I have an other question if you don't mind.
Since we're using the maxmind database with the STM Tracker V3/V4 and like you said, redirection based on country already works like a charm...
Is there a simple way to display the country in our landing pages starting from this setup?
PS: thank you for your answer above 
this is SO fucking gangster.
Wow this is huge!
Thanks kyuss!
So I'm trying to get this working and followed all of the steps above. When I write the php code, I get the following errors. Any suggestions?
Warning: require(vendor/autoload.php) [function.require]: failed to open stream: No such file or directory in .......
Fatal error: require() [function.require]: Failed opening required 'vendor/autoload.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in ......
Thanks
Beauty! Will this work with mobile?
@drumminlogan - Hmm, that sounds like there's a problem with the installation of the MaxMind databases. Is there a "vendor" directory in the same directory as your LP?
You may need to modify the "require" line to include an absolute path to the vendor directory - something like
require '/var/www/vendor/autoload.php';
require '/usr/local/apache/htdocs/vendor/autoload.php';
I had your problem. Try ssh into the user directory where your LP is held. Also, you have to run the installation at the LP root. eg ~user/public_html/mytestlp/
I'm still trying to figure out how I can just use one install and have it shared among different LPs.
On another note, I've been testing with this Geo script for my UK offer, but the level of detail is kinda not worth it. Just says London, which doesn't really make a compelling sell because London is so vague and general.
Ideally, I would like to see a postcode (I know the US database has it) or at least a town more specific.
I'm not even sure if it's worth adding for my UK offer, if it's just going to say London all the time. Thoughts?
@quantum27 - IP-based geoscripts in the UK are a total pain in the arse, I'm afraid. For various reasons, they're usually imprecise and sometimes flat-out wrong, thanks to the locations that ISPs run from against the location that users are in.
I don't know of a way to locate a user by postcode in the UK aside from using HTML5 geolocation - and you'll have to get them to agree to that before the results come up.
@drumminlogan - Hmm. Try copying your GeoLite2-City.mmdb file into the same directory as your LP - that might well solve it. (Let me know!)
I have another solution which will probably work if that doesn't, but it's a bit more of a pain.
Hurrah! Excellent.
Quick tip on the "copying" front - if you have SSH access to your server, you could actually just use symbolic links to the file. Here's a decent tutorial on that. That'll make the process easier and neater than copying over every time.
OK, here's a MUCH better solution, courtesy of a PM from an extremely helpful STMer:
Replace the starting block of the original code with
<?PHP
require 'vendor/autoload.php';
$ipaddress = $_SERVER["REMOTE_ADDR"];
use \GeoIp2\Database\Reader;
$reader = new Reader('/PATH_TO_YOUR/GeoLite2-City.mmdb');
$record = $reader->omni($ipaddress);
$subid = $_GET['subid'];
switch($record->country->isoCode) {
Note: tutorial updated to provide an elegant solution to the above bug. Should Just Work now!