If you do any CPA marketing you’ll notice that most of the offers are restricted to certain countries or have different links depending on the country. Here’s what I do to redirect visitors to different links depending on the country as determined by the visitor’s IP address.
First you will need to install the PHP Net_GeoIP package onto your host. Most hosting providers already have this installed including the one’s I use Beyond Hosting and HostGator.
Next you will need to grab the latest GeoLite Country database binary from MaxMind here. Extract the archive and place the ‘GeoIP.dat’ file in the same directory as the script.
If you use Beyond Hosting, place it here:
/home/your-domain/php/Net/GeoIP.dat
Now comes the actual redirect script itself which we’ll call ‘redirect.php’.
<?php // Next two lines are for Beyond Hosting // Don't forget to change your-domain require_once '/home/your-domain/php/Net/GeoIP.php'; $geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat'); // Next two lines are for HostGator require_once 'Net/GeoIP.php'; $geoip = Net_GeoIP::getInstance('GeoIP.dat'); try { $country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']); switch((string)$country) { case 'AU': $url = "http://www.howwhowhen.com"; break; case 'CA': $url = "http://www.howwhowhen.com"; break; case 'GB': $url = "http://www.howwhowhen.com"; break; case 'US': $url = "http://www.howwhowhen.com"; break; default: $url = "http://www.howwhowhen.com"; } header('Location: '.$url); } catch (Exception $e) { // Handle exception } ?>
Make sure you only include the section for Beyond Hosting or Hostgator only. Then change the URLs to your offer links. Now when a visitor goes to www.your-domain.com/redirect.php they will be redirected to the proper links.
If you have any questions just leave a comment below.
web hosting says
Wonderful work! This is the kind of info that should be shared around the web. Shame on Google for not positioning this publish upper! Come on over and talk over with my website . Thanks =)
Howie Nguyen says
Hi Bo, if you want it to hit your script right when they get to your domain then just name the script index.php and place it at the root of your domain.
As far as integrating with WordPress can you give me an example of what you’re trying to do? I’ll see if I can help. Otherwise you might want to take a look at this:
CPA Geo Plus
It’s a WP plugin and might be what you’re looking for. I don’t use it myself as I don’t have any need for it though.
Mike says
Thanks for the script and easy setup. I’m stuck on a few things if you could assist?
I have BeyondHostingt and it says to place the GeoIP.dat file in /home/your-domain/php/Net/GeoIP.dat. Are you referring to creating new folders within my domain for this? Or is this done in the Cpanel file manager?
Howie Nguyen says
Mike, you shouldn’t have to create any folders. You can use cPanel to navigate to /home/your-domain/php/Net/ and then upload the file GeoIP.dat there.
Mike says
Thanks for your fast reply Howie! I owe you for the support and I found this page from the EWA group. I opened Cpanel and found /home/my-domain/ but unfortunately there was no php folder after that. Sounds like I might need to contact Tyler at beyond unless you know of another solution.
Howie Nguyen says
In cPanel under Software/Services, open PHP Pear Packages. Then install Net_GeoIP. That should create the directories and install the needed packages for you.
Tyler from BeyondHosting is awesome along with the rest of their support team. They might tell you that you don’t need to install anything or do any of this but what I found out is the stuff on their VPS (at least the Hybrid) does not contain the latest database. Doing it this way you can always have the latest database with accurate information.
Mike says
Howie you are the man! I truly appreciate your help. Turns out I just needed to install the PHP Pear Packages which I’ve never done previously to this. I tested the script with about 5 countries using VPN and it works like a charm.
Thank you so much!