0

我有一个网站,自动检测国家并根据国家设置语言,但用户无法手动更改它。

如何将以下代码添加到它下面的代码中以确保我可以输入以下链接:http ://example.com/index.php?lang=de

//Code I want to integrate with the code below

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  case 'de':
  $lang_file = 'lang.de.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

//Main Code Below

<?php

function visitor_country()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    $result  = "Unknown";
    if(filter_var($client, FILTER_VALIDATE_IP))
        {
            $ip = $client;
        }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
        {
            $ip = $forward;
        }
    else
        {
            $ip = $remote;
        }

    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));

    if($ip_data && $ip_data->geoplugin_countryName != null)
        {
            $result = $ip_data->geoplugin_countryName;
        }

    return $result;
}

session_start();
header('Cache-control: private'); // IE 6 FIX

if (isSet($_GET['lang'])) {
    $lang = $_GET['lang'];

    // register the session and set the cookie
    $_SESSION['lang'] = $lang;

    setcookie('lang', $lang, time() + (3600 * 24 * 30));
} else if (isSet($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
} else if (isSet($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
} else {
    $lang = 'en';
}

if(visitor_country() == "Germany") {
    $lang_file = 'lang.de.php';
    //echo "Germany";
} else {

    $lang_file = 'lang.en.php';
    //echo "Not in Germany";
}

include_once 'languages/' . $lang_file;

?>

有什么建议么?

4

2 回答 2

2

将自动检测放在default:交换机的情况下:

$lang = null;
if (isset($_GET['lang'])) {
    $lang = $_GET['lang'];

    // register the session and set the cookie
    $_SESSION['lang'] = $lang;

    setcookie('lang', $lang, time() + (3600 * 24 * 30));
} else if (isset($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
} else if (isset($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
}

switch ($lang) {
case 'en':
    $lang_file = 'lang.en.php';
    break;

case 'de':
    $lang_file = 'lang.de.php';
    break;

default:
    if(visitor_country() == "Germany") {
        $lang_file = 'lang.de.php';
        //echo "Germany";
    } else {

        $lang_file = 'lang.en.php';
        //echo "Not in Germany";
    }
}
于 2013-11-03T18:13:30.523 回答
0
if(isset($_GET['lang'])){
   // get variable 'lang' is defined.
   $lang = $_GET['lang']
}

$lang_file您在此处的代码中重置以下值 :

if(visitor_country() == "Germany") {
    $lang_file = 'lang.de.php';
    //echo "Germany";
} else {

    $lang_file = 'lang.en.php';
    //echo "Not in Germany";
}

如果您删除该代码,它应该可以工作。

于 2013-11-03T18:10:54.653 回答