问题
I've integrated two pieces of PHP code found on different websites. The first part of code finds out what country you are in and the second one determines the language. I have tried running it but with no luck. It does not give me a error, just a blank white screen.
Here is the code:
<?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';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'de':
$lang_file = 'lang.de.php';
break;
if(visitor_country() == "Germany") {
default:
$lang_file = 'lang.de.php';
echo "Germany";
} else {
default:
$lang_file = 'lang.en.php';
echo "Not in Germany";
}
}
include_once 'languages/' . $lang_file;
?>
I could not include the language code, but rest assured it works as I use it on my site without auto-country detection.
回答1:
You code returns an error: PHP Parse error: syntax error, unexpected 'default' (T_DEFAULT) in yourfile.php on line
. It means that you're misusing default
in switch statement. Replace your last part of your code with this:
default:
if(visitor_country() == "Germany") {
$lang_file = 'lang.de.php';
echo "Germany";
} else {
$lang_file = 'lang.en.php';
echo "Not in Germany";
}
Take out default
from inside of if/else
statement.
EDIT 1:
Make sure that PHP displays ERRORS
, WARNING
and NOTICES
to properly debug your code:
ini_set('display_errors', -1);
EDIT 2:
If it was working without the switch
statement, like you said, then you must make sure that files lang.en.php
/lang.de.php
really exist.
EDIT 3:
You are suppressing errors with having @
in-front of json_decode(file_get_contents
. Most likely you would have to edit your php.ini
and enable allow_url_fopen
to make it work. I bet if you remove @
, you will get an error:
Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in
I wouldn't strongly recommend using this kind of method to detect user language and enabling allow_url_fopen
as it's possible security flaw! In case you're interested, I will provide you with much better solution for determining user browsers' language.
来源:https://stackoverflow.com/questions/19755290/determining-user-language-based-on-ip-address-geolocation