Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 412
Leb Avatar asked Nov 20 '25 13:11

Leb


1 Answers

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");
}


?>
like image 131
AEQ Avatar answered Nov 23 '25 04:11

AEQ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!