0

I am making a website in 2 languages. Is it possible to check it in which country the user is, so that I can switch the language automatically for the user? I also have a switch option in my menu with a _GET. I have this piece of code in the beginning:

if (isset($_GET['taal'])) {
    $taal = $_GET['taal'];
} else {
   $taal = 'ENG';
}
switch ($taal) {
    case 'NL':
        include('NL.php');
        break;
    case 'ENG':
        include('ENG.php');
        break;
    default:
        include('ENG.php');
        break;
}
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
William
  • 156
  • 1
  • 3
  • 11
  • 1
    http://php.net/manual/en/function.geoip-country-code-by-name.php – Chris Mar 24 '16 at 15:41
  • Do you have a problem with the code you included in the question, or is the question really just "how do you find the user's country" and the code is only there as an example of what you're going to do with it? – Don't Panic Mar 24 '16 at 15:42
  • It is more "how do you find the user's country" than the connection. But the code is more for the connection @Don'tPanic – William Mar 24 '16 at 15:45
  • 1
    I think what you are looking for is [this](http://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip). – Mawia HL Mar 24 '16 at 15:46
  • 1
    You are better off using their language preferences which can be obtained from the headers (Accept-Language) as someone might be in Spain for example but doesn't actually speak Spanish and has their browser set to English. – mbx-mbx Mar 24 '16 at 15:49
  • 1
    Don't do an IP lookup, that's a lot of overhead for something not very accurate. Instead trust that the client's browser is telling you what language it wants, which you can do by checking the request headers, e.g., `Accept-Language:en-US,en;q=0.8` – Alex Howansky Mar 24 '16 at 15:50
  • @MawiaHL Do you know how i can change my code with [this link](http://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip) – William Mar 24 '16 at 15:57

2 Answers2

1

I would like to point you to a similar question with a relevant answer.

As you can tell from this answer the first step to identifying a user's location/country would be to read their IP address. This can sometimes be a little tricky due to proxies. A good way to identify a user's IP using PHP can be found here.

Also, if you're looking to create a multi-language platform you may want to first read, understand and adhere to basic industry standards. If you wanted to, you could use PHP for this like you're already doing; or you can use universal language files which would have the benefit of being cross-platform (PHP,Java,C# etc.).

Community
  • 1
  • 1
C_B
  • 2,620
  • 3
  • 23
  • 45
1

First you need to get the user country by ip address:

$country = ip_info($_SERVER['REMOTE_ADDR'], 'Country');
if($country=='Netherlands'){
   $taal = 'NL';
}else{
   $taal = 'ENG';
}
switch ($taal) {
    case 'NL':
        include('NL.php');
        break;
    case 'ENG':
        include('ENG.php');
        break;
    default:
        include('ENG.php');
        break;
}
Community
  • 1
  • 1
Mawia HL
  • 3,605
  • 1
  • 25
  • 46