简体   繁体   中英

Detecting browser language in PHP and redirect

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

?>

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

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!

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

?>

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');
}
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM