1

I want to identify the visitor's region. My website has 4 language versions and I want to detect whether a visitor is visiting my site from English speaking country or russian, or arabic or spanish speaking country. What is the best way to do it?

3
  • use $_SERVER['REMOTE_ADDR']; and on the basis of IP list do the following to your visitors
    – swapnesh
    Oct 12, 2012 at 11:06
  • I hate websites that do that. Give the user the choice to select whichever language he wants. Oct 12, 2012 at 11:21
  • You should use HTTP_ACCEPT_LANGUAGE and also offer the choice of setting language manually. GEOIP doesn't always work as people visit through proxies/VPNs/corporate gateways which don't reflect what language they actually speak let alone telling you where they are located!
    – nickhar
    Oct 12, 2012 at 11:43

3 Answers 3

1

This is what we use in our scripts to do similar detection:

function get_preferred_lang() {
    if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
        preg_match_all('#([a-z-]+) *(?:; *q=([0-9.]+))?#', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]), $matches);
        $accept = array_combine($matches[1], $matches[2]);
        foreach ($accept as $key => $val)
        $accept[$key] = $val == '' ? 1.0 : (float) $val;
        arsort($accept);
        return substr(array_shift(array_keys($accept)),0,2); 
      } else {
        return 'unknown';
    }
}
0

Please Use MaxMind API for PHP:

INSTRUCTIONS

DOWNLOADS

-1

You can use $_SERVER['REMOTE_ADDR']; to fetch user IP address and on the basis of this use this link to proceed further according to your requirement GEOIP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.