-1

I want to write a simple if statement using HTTP_ACCEPT_LANGUAGE function that redirects based on result of what the users browser language is. I am still a beginner so am obviously keeping it as simple as possible. This is what I have so far but the "if" statement needs work. Can anyone help me with a fix?

<?php
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if ($lang=german) {
    header("Location: http://www.example.com/german/index.html");
    } else if ($lang=spanish) {
        header("Location: http://www.example.com/spanish/index.html");
        }
        else if ($lang=french) {
            header("Location: http://www.example.com/french/index.html");
            }
            else if ($lang=chinese) {
                header("Location: http://www.example.com/chinese    /index.html");
                    } else {
                    echo "<html>english content</html>";
                    }

?>
armani
  • 111
  • 1
  • 1
  • 3
  • 1
    Use a `switch` statement, it is way more readable than this. – Bart Friederichs Aug 07 '13 at 08:37
  • What do you mean "needs work". Does it work, are you having errors, what's the problem/question? – Bart Friederichs Aug 07 '13 at 08:39
  • You can use this example code from the same question in [1]: http://stackoverflow.com/a/3770616/2097224 – Black Sheep Aug 07 '13 at 08:41
  • the language references are just placeholder text I used because I don't know what goes in the "if" statement between parenthesis. By "needs work" I mean that I am sure that this will not execute correctly. – armani Aug 07 '13 at 08:45

6 Answers6

4

I don't know what your language literals are, so I'd say make them ISO language codes.

Use a switch statement, this is more readable and smaller:

$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
switch($lang) {
    case "de-DE":
    case "es-ES":
    case "cn-CN":
    case "fr-FR":
        header("Location: http://www.example.com/$lang/index.html");
        break;
    default:
        header("Location: http://www.example.com/en-US/index.html");
        break;
}

Further, you are assigning, not comparing. You compare with ==:

if ($lang == "de-DE")
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Bart: Thank you! For arguments sake, if I was to not use your switch statements - which do look better than mine by the way - and stay with mine, would if ($lang == "de-DE") work for my purposes? – armani Aug 07 '13 at 08:54
  • Probably (why don't you try it out?), note that I guessed the string literals. They might be a little different, like `de_de` or just `de`. Point in the `if` way is that you use `==` for comparison. – Bart Friederichs Aug 07 '13 at 09:03
  • I haven't tried it because I only have an English browser... Is there a way around this for testing purposes? – armani Aug 07 '13 at 10:49
  • @armani Firefox has the option to switch languages. Options > Content > Languages. – Bart Friederichs Aug 07 '13 at 11:09
  • Bart: I tried your solution but the code does not work. It defaults to the second header when using a russian firefox installation. I tried to "echo" out $_SERVER['HTTP_ACCEPT_LANGUAGE'] and this is what comes back : ru,en-us;q=0.7,en;q=0.3 Any Ideas? – armani Aug 07 '13 at 12:00
  • By the way, I am using four cases in the switch statement as above. They are: fr, de, cn and ru. I am not using spanish anymore. I got these from the ISO language codes you posted a link so am sure they are correct. – armani Aug 07 '13 at 12:03
  • I had to use a CONTAINS (strpos) instead of a literal comparison – dialex Jul 12 '15 at 15:01
2

Assuming you always redirect to /language/, you could do it this way:

<?php 
    $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if ( in_array( $lang,array("german","spanish","french","chinese") ) ) {
       header("Location: http://www.example.com/$lang/index.html");
    } else {
        echo "<html>english content</html>";
    }

?>

Also, the comparisons in your if need to be done with ==, it's assignment otherwise!

brasofilo
  • 25,496
  • 15
  • 91
  • 179
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • Mbaas: Thank you but I tried your solution as well. the redirect does not work. It executes the "else" statement and I have a german and chinese browser. Neither of them work. If I land on the domain with a Chinese browser, I should get redirected to the Chinese version of the site. Why is it not working :( – armani Aug 07 '13 at 16:07
0

Try this:

<?php
$path = array(
    'en-US' => 'english',
    // etc
);
$accepts = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (in_array($accepts[0], $path)) {  // if path exists for language then redirect to path, else redirect to default path (english)
    header('Location: http://www.example.com/' . $path[$accepts[0]] . '/index.html');
} else {
    header('Location: http://www.example.com/english/index.html');
}
?>
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
0

HTTP_ACCEPT_LANGUAGE returns not "english", but two signs symbol like "en", or region and language symbol like "en_us". You shouldn't use if statement it's hard to read. You should use array (in future you can simple write it to config files, or move to databases). The proper code should look that:

$default_lang = 'en';
$lang_redirectors = array('de' => 'http://www.example.com/german/index.html', 
                          'en' =>   'http://www.example.com/english/index.html');
function redirect($url){
    header("Location: " . $url);
}

$hal = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$langs = explode($hal, ',');

foreach($langs as $lang){
    $lang_prefix = substr($lang, 0, 2);
       if(in_array($lang_prefix, $lang_redirectors)){
           redirect($lang_redirectors[$lang_prefix]);
           break;
       }
    redirect($lang_redirectors[$default_lang]);
}
whncode
  • 429
  • 3
  • 15
  • This script does not work. The redirect function needs `$url` and the concatenation is with `.` not `+` – Joseph Jun 13 '14 at 15:32
0
<?php
$browserlang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$lang = $browserlang[0] . $browserlang[1] . $browserlang[2] . $browserlang[3] . $browserlang[4];


if (($lang=="sk_SK") OR ($lang=="sk-SK")) {
    header("Location: https://www.example.sk/sk");
}
else if (($lang=="en_EN") OR ($lang=="en-EN") OR ($lang=="en_GB") OR ($lang=="en_US") OR ($lang=="en-GB") OR ($lang=="en-US"))  {
    header("Location: https://www.example.sk/en");
}
else {
    header("Location: https://www.example.sk/en");
}

?>
0
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch($lang) {
case "ru":
    header("Location: https://example.com/ru");
    break;
case "ka":
    header("Location: https://example.com/ka");
    break;
default:
    header("Location: https://example.com/en");
    break;