【问题标题】:automatic language selection and forwarding to file自动选择语言并转发到文件
【发布时间】:2013-06-19 21:20:10
【问题描述】:

我想在我的 index.php 中自动选择语言。当用户来自哥伦比亚时,他会被重定向到 index_columbia.html,而其他国家/地区会被重定向到 index_english.html。

我怎样才能用 PHP 做到这一点?

【问题讨论】:

    标签: php selection country


    【解决方案1】:

    浏览器发送的语言信息带有服务器保留变量。好吧,这个解决方案不是基于访问者的位置,而是基于浏览器的语言设置,这似乎更好。如果访问者来自哥伦比亚或法国并不重要,如果他使用英语作为一种语言,最好向他展示网站的英文版本。

    你可以像这样使用$_SERVER['HTTP_ACCEPT_LANGUAGE']:-

    <?php
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    switch ($lang){
        case "fr":
    
            include("index_fr.php");
            break;
        case "it":
    
            include("index_it.php");
            break;
        case "en":
    
            include("index_en.php");
            break;        
        default:
    
            include("index_en.php");
            break;
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      您可以通过 IP 地址检查(有网络服务)。例如:http://freegeoip.net/

      您也可以使用浏览器发送的接受语言标头。 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 这可以从$_SERVER 变量中检索到。

      【讨论】:

        【解决方案3】:

        您可以通过 IP 地址使用国家/地区查找。

        可以在http://www.phpandstuff.com/articles/geoip-country-lookup-with-php 找到执行此操作的类。

        【讨论】:

          【解决方案4】:

          你可以使用我的代码:

          $langs = array('en','fr','de');
          $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
          if (!in_array($lang, array_keys($langs))) $forward = 'index_'.$lang.'.html';
          else $forward = 'index_en.html';
          

          【讨论】:

          • 如何准确地使用它?
          • header("状态:301 永久移动"); header("位置:mydomain.com/".$forward); 退出;
          猜你喜欢
          • 1970-01-01
          • 2021-07-13
          • 1970-01-01
          • 2017-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多