Geolocation Whitelisting/Blacklisting

Sometimes I get a request to either blacklist some countries from viewing content due to copyright restrictions or to only allow specific countries to access some of the site’s content. I had a bit of a tough time trying to figure out the best method to get the IP ranges.

In order to block them, I had to first decide how I wanted to implement it. Should I do it right from Apache? Perhaps through the .htaccess file? The best solution that I’ve found so far is to use the free API of hostip.info.  With a quick cURL request in PHP to their servers, I can pass an IP address to them and have it return a 2 letter Country Code such as US. Using that data, I can then either whitelist or blacklist certain page elements depending upon how I implement it.

I’ve written a function called getTheIpAddress() that will get an ip address from the person accessing the website. First it will check the client’s IP address. If this is empty, it will grab a proxy’s id. Lastly, if this one doesn’t exist either, it will grab the ip address of the server executing the request:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function getTheIpAddress() {
  // Check client's ip
  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
 
  // Is this from a proxy?
  } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
 
  // All else fails, grab the remote ip
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
 
  return $ip;
}

Additionally, I set a variable letting me choose to check and IP address from the function provided or from the GET variable (if I wanted to test an IP manually).

1
2
3
4
5
6
7
$useGetIp  = false;
if ($useGetIp) {
  $visitorIp  = cleanInput($_GET['ip']);
 
} else {
  $visitorIp  = getTheIpAddress();
}

Once I have the IP address, I do the cURL request and change the action based on which countries I’m going to allow to see the content. In this specific example, I made it so that specific Asian countries would be forwarded to a different website:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Make a request to find out where the visitor is coming from
$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://api.hostip.info/country.php?ip='.$visitorIp);
  // Tell curl to make the result a variable
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // Execute the curl session
  $geoLoc = curl_exec($ch);
  // Close the curl session
  curl_close ($ch);
 
# Get the right link for the country the visitor resides in
switch($geoLoc) {
  //_________________________ Asian Countries
  case "BN":
  case "CN":
  case "HK":
  case "ID":
  case "KH":
  case "KP":
  case "KR":
  case "LA":
  case "MM":
  case "MO":
  case "MY":
  case "PH":
  case "SG":
  case "TH":
  case "TW":
  case "VN":
    $landingPage  = 'http://www.example2.com';
    header("Location: {$landingPage}");
    break;
 
  //_________________________ All Other Countries
  default:
    // Do nothing, this is the normal homepage
    $landingPage  = 'http://www.example.com/';
    header("Location: {$landingPage}");
    break;
}

And there you have it, geolocation based information! You can use this script in any number of ways. IT’s build right now to be at the top of a landing page to direct traffic but it could be easily modified to work with specific sections of content.

Let me know if you decide to use it!

Tags: ,

One Response to “Geolocation Whitelisting/Blacklisting”

  1. sakutaco April 22, 2010 at 3:12 pm #

    Now I wish I knew how to program so I could make sense of this!

Leave a Reply