Extreme Thinking
php網頁判斷瀏覽器語系

2017-10-27


找到一個外國人寫的判斷語系 , 的 sitemap , 就有文章說 , 那網頁能判斷 瀏覽器語系 然後簡體字就簡體 , 繁體瀏覽器就繁體 , 日語瀏覽器就日語 , 其他就英文 ……..

我家網頁是用 session 內的變數 lanage 來切換語系的 ….那就改一改 , 將初始化改直接定義 _session[‘lanage’] 為瀏覽器語系

原本是 :

session_start();
if(!isset($_SESSION['language'])){
    // if null set _SESSION['language']="Chinese"
    $_SESSION['language']="Chinese";
}
switch ($_SESSION['language']) {
    case "Japanese":
        //echo "Japanese";
        include_once('Jp.php');
        break;
    case "English":
        //echo "English";
        include_once('En.php');
        break;
    case "Simp":
        //echo "Simp";
        include_once('Sc.php');
         break;
    default:
         //echo "Chinese";
         include_once(Ch.php');
 }

改成

session_start();
if(!isset($_SESSION['language'])){
    switch (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) {
        case "zh":
            $_SESSION['language'] = "Simp";
            if (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5) == "zh-tw")
                {$_SESSION['language'] = "Chinese"; }
            if (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5) == "zh-TW")
                {$_SESSION['language'] = "Chinese"; }
            break;
        case "ja":
            $_SESSION['language'] = "Japanese";
            break;
        default :
            $_SESSION['language'] = "English";
    }
}
switch ($_SESSION['language']) {
    case "Japanese":
        //echo "Japanese";
        include_once('Jp.php');
        break;
    case "English":
        //echo "English";
        include_once('En.php');
        break;
    case "Simp":
        //echo "Simp";
        include_once('Sc.php');
         break;
    default:
         //echo "Chinese";
         include_once(Ch.php');
  }