Leb
Leb

Reputation: 117

PHP Redirect just once with GeoIp2

I am using MaxMind's GeoIp2 PHP to redirect website visitors based on their country.

I have managed to get the redirect working so that:
US visitors go to http://www.example.com/us
Malaysian visitors go to http://www.example.com/my
All other visitors go to http://www.example.com

The problem is that I only want to redirect visitors once. After they are on the website, if they navigate to http://www.example.com they should be able to do so without getting redirected, regardless of their country.

This is so that both humans and spiders can still have the freedom to visit pages that are not targeted at their country.

I have tried using the suggestion to a similar problem as answered here but the question there is regarding different domains for different countries instead of different paths so the solution doesn't work for me.

The code:

<?php 
require_once '../vendor/autoload.php';
use GeoIp2\Database\Reader;

$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-Country.mmdb');

$record = $reader->country( $_SERVER['REMOTE_ADDR'] );

try {
  $country = $record->country->isoCode;

  switch((string)$country) {
    case 'US':
      $url = "http://www.example.com/us";
      break;
    case 'MY':
      $url = "http://www.example.com/my";
      break;
    default:
      $url = "http://www.example.com";
  } 

  if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
  {
      header("Location: ".$url);
  }

} catch (Exception $e) {
  // Handle exception
}
?>

Any help is greatly appreciated.

Upvotes: 3

Views: 509

Answers (1)

AEQ
AEQ

Reputation: 1429

You could use a cookie to keep track of:

  1. if the visitor has been redirected before
  2. the country that the visitor has been redirected to before

If the spiders are clever, they will make use of the cookies too (Ref: Can Bots/Spiders utilize Cookies?).

So you could write your logic like so:

<?php 
require_once '../vendor/autoload.php';
use GeoIp2\Database\Reader;

$cookie_name = "country_code";
session_start();


if (isset($_GET['check']) && $_GET['check'] == true) {

  if (isset($_COOKIE['test_cookie']) && $_COOKIE['test_cookie'] == 'test') {

    if(!isset($_COOKIE[$cookie_name])) {

      $reader = new Reader('/usr/local/share/GeoIP/GeoLite2-Country.mmdb');

      $record = $reader->country( $_SERVER['REMOTE_ADDR'] );

      try {

        $country = $record->country->isoCode;

        switch((string)$country) {
          case 'US':
            $url = "http://www.example.com/us";
            break;
          case 'MY':
            $url = "http://www.example.com/my";
            break;
          default:
            $url = "http://www.example.com";
        } 

        $cookie_value = "" . (string)$country;

        setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
        if(!isset($_GET['cookies'])){

          header('Location:/info.php?cookies=true');
        }
        if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
        {
            header("Location: ".$url);
        }

      } catch (Exception $e) {
        // Handle exception
      }

    } else { //cookie is set no redirect

    }

  } else { //no cookie support, no redirect

  }

} else {

  setcookie('test_cookie', 'test', time() + 3600);

  header("location: {$_SERVER['PHP_SELF']}?check=true");
}


?>

Upvotes: 2

Related Questions