0

EDIT: Wasn't a duplicate to began with. Originally was how to improve or use different code to redirect based on the visitor's COUNTRY.

Here is the code I'm currently using:

require_once('geo/geoip.inc');
$gi = geoip_open('geo/GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

if ($country == 'FR') { 
header('Location: http://fr.mysite.com');
}

if ($country == 'US') { 
header('Location: http://us.mysite.com');
}

on a couple of static (html + javascript) viral sites that bring in around 100,000 visitors a day, sometimes more and sometimes less.

Are there any better (and free) solutions to redirect based on country? I recently switched from a dedicated server to a VPS and the current code seems to use a lot of CPU usage (or so I'm told by my host). I'll probably just go back to the dedicated server but I would still like to know if there is a better way, one that doesn't stress the server as much.

Also, since I'm redirecting based on language:

// french
if ($country == 'FR') { 
header('Location: http://fr.mysite.com');
}

//french
if ($country == 'BE') { 
header('Location: http://fr.mysite.com');
}

//french
if ($country == 'CA') { 
header('Location: http://fr.mysite.com');
}

//english
if ($country == 'US') { 
header('Location: http://us.mysite.com');
}

//english
if ($country == 'UK') { 
header('Location: http://us.mysite.com');
}

and extremely tired right now, what's the better way? this or no:

    //english
    if ($country == 'US') || ($country == 'CA') { 
    header('Location: http://us.mysite.com');
    }

So anyone visiting from US or Canada would be redirected to google.com

Thanks in advance

xetsr
  • 35
  • 7
  • just send to google.com, it will redirect to the country sites for you. or use the browser language. –  Jun 09 '16 at 23:50
  • My fault, I was just using google as an example. I would be sending visitors to other pages or subdomains of my site based on country. What would be the best way for get browser language and redirect to the appropriate page? Of course I'm gonna google it later tonight but if you have any suggestions let me know. Thanks! – xetsr Jun 09 '16 at 23:52
  • downvote for code your not actually using –  Jun 09 '16 at 23:55
  • not sure why it matters what the URL is but whatever, that's cool – xetsr Jun 10 '16 at 00:03
  • Use `elseif` when the cases are mutually exclusive. Or use an associative array whose keys are the country codes. – Barmar Jun 10 '16 at 00:04
  • this actually is a pretty spot on duplicate question. you're asking for a better way to choose between a set of country codes. instead of a series of `if` statements, use a `switch` like in the duplicate question `switch ($lang_var){ case "fr":` – Jeff Puckett Jun 10 '16 at 03:34
  • 1
    the only difference is the source of data. you're using location based, and the other system is using the browser's preferred language. if you think about it, the preferred language is probably better than location because people living in the US may prefer a different language site than your `us.mysite.com` unless the site is geographically sensitive. but I'm guessing it's about language translation, and you're going to get better information from the browser about that for the end-user than you will from the country their IP is sourced. – Jeff Puckett Jun 10 '16 at 03:35
  • Never ever use geolocalization for language detection. A reason? well, just travel somewhere, or try it in India/Canada/Switzerland (with per-region locale). – Adrian Maire Jan 27 '22 at 17:38

1 Answers1

1

EDIT: Source ended up being an answer to the duplicate question.

If the only differences between the sites are languages and nothing to do with their actual country, you should be redirecting based on preferred language instead.

However, this gets really complex, as HTML headers can contain multiple languages, with certain languages preferred over others. A solution I found a while back, and unfortunately cannot find the original source to is to make a list of available languages, parse the languages out of the HTML Header with their order of preference, and determine based on that which redirect to follow:

I DO NOT OWN THE prefered_language() FUNCTION AND DID NOT TAKE ANY PART IN ITS CREATION! But I can't find the original anywhere.. If someone else is able to, please, link it...

$available_languages = array("en", "fr");

$default_language = "en";

function prefered_language($available_languages, $http_accept_language) {
    global $default_language;
    $available_languages = array_flip($available_languages);
    $langs = array();
    preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER);
    foreach($matches as $match) {
        list($a, $b) = explode('-', $match[1]) + array('', '');
        $value = isset($match[2]) ? (float) $match[2] : 1.0;
        if(isset($available_languages[$match[1]])) {
            $langs[$match[1]] = $value;
            continue;
        }
        if(isset($available_languages[$a])) {
            $langs[$a] = $value - 0.1;
        }
    }
    if($langs) {
        arsort($langs);
        return key($langs);
    } else {
        return $default_language;
    }
}

if(isset($_COOKIE["client_lang"])){
  $lang = $_COOKIE["client_lang"];
}else{
  $lang = prefered_language($available_languages, strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
  setcookie("client_lang", $lang, time() + (86400 * 30 * ), "/");
}

header('Location: http://' . $lang . '.mysite.com');

I'd also suggest creating a cookie for the client's preferred language as I have done above, since this function will still require some CPU usage.

Community
  • 1
  • 1
Arix Zajicek
  • 58
  • 1
  • 1
  • 9