PHP 检测浏览器语言环境

阅读(2961)

PHP 拿到 $_SERVER 的 HTTP_ACCEPT_LANGUAGE值,来判断应该输出的语言。
实际环境中,初次判断后写入cookie,也允许用户选择某种语言后更新 cookie,每次在服务端读取cookie来判断就行了。

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
    case "fr":
        //echo "PAGE FR";
        include("index_fr.php");//include check session FR
        break;
    case "it":
        //echo "PAGE IT";
        include("index_it.php");
        break;
    case "en":
        //echo "PAGE EN";
        include("index_en.php");
        break;        
    default:
        //echo "PAGE EN - Setting Default";
        include("index_en.php");//include EN in all other cases of different lang detection
        break;
}
?>
Tags: