I am using the php geoip_country_code_by_name function to serve up different content for different countries from an array which looks like this:
我使用php geoip_country_code_by_name函数为不同国家提供不同的内容,而数组如下所示:
<?php
$content = array(
'GB' => array(
'meta_description' => "Description is here",
'social_title' => "Title here",
'country_content_js' => "js/index.js",
),
'BR' => array(
'meta_description' => "Different Description is here",
'social_title' => "Another Title here",
'country_content_js' => "js/index-2.js",
),
);
?>
How can I check if the users country is in the array and if not set 'GB' as the default?
如何检查用户国家是否在数组中,如果没有将'GB'设置为默认值?
I am using this to check for the country:
我用这个来检查这个国家:
$country = ( isset($_GET['country']) && !empty($_GET['country']) ? $_GET['country'] : ( isset($_SESSION['country']) && !empty($_SESSION['country']) ? $_SESSION['country'] : ( isset($_COOKIE['country']) && !empty($_COOKIE['country']) ? $_COOKIE['country'] : geoip_country_code_by_name(ip()) ) ) );
3 个解决方案
#1
0
First : i Added a new variable for default country code .. ($defaultCountry = 'GB');
首先:我为默认国家代码添加了一个新变量。($ defaultCountry = ' g ');
Second : try to get the country code from (get , session , cookie ,
geoip_country_code_by_name or default as assigned).
第二:尝试从(get、会话、cookie、geoip_country_code_by_name或指定的默认值)获取国家代码。
Finally : check if the country code exists in the $content array() otherwise return the default country ..
最后:检查$content数组()中是否存在国家代码,否则返回默认国家。
$defaultCountry = 'GB';
if(isset($_GET['country']) && !empty($_GET['country'])){
$country =$_GET['country'];
}elseif(isset($_SESSION['country']) && !empty($_SESSION['country'])){
$country =$_SESSION['country'];
}elseif(isset($_COOKIE['country']) && !empty($_COOKIE['country'])){
$country =$_COOKIE['country'];
}elseif($value = geoip_country_code_by_name(ip())){
$country = $value;
}else{
$country = $defaultCountry;
}
if(isset($content[$country])){
$country =$content[$country];
}else{
$country = $content[$defaultCountry];//Default ..
}
#2
1
First check if the country code is in the $content
array as a key or not, if not serve the first array as a default. To check if the key exists in array or not use array_key_exists().
首先检查$content数组中的国家代码是否是键,如果不将第一个数组作为默认值提供的话。要检查键在数组中是否存在,请使用array_key_exists()。
Like this,
像这样,
$countrycode="IN";
if(!array_key_exists($countrycode,$content)) {
$countryarray=$content[0];
} else {
$countryarray=$content[$countrycode];
}
Above code will return country's content if available or first if not found in an array.
如果可用,以上代码将返回国家的内容;如果不在数组中,则返回国家的内容。
#3
0
You can check it with ternary operator
also
你也可以用三元算符检查
countryArr = array();
$countryArr = array_key_exists($code,$content) ? $content[$code] : $content['GB'];
#1
0
First : i Added a new variable for default country code .. ($defaultCountry = 'GB');
首先:我为默认国家代码添加了一个新变量。($ defaultCountry = ' g ');
Second : try to get the country code from (get , session , cookie ,
geoip_country_code_by_name or default as assigned).
第二:尝试从(get、会话、cookie、geoip_country_code_by_name或指定的默认值)获取国家代码。
Finally : check if the country code exists in the $content array() otherwise return the default country ..
最后:检查$content数组()中是否存在国家代码,否则返回默认国家。
$defaultCountry = 'GB';
if(isset($_GET['country']) && !empty($_GET['country'])){
$country =$_GET['country'];
}elseif(isset($_SESSION['country']) && !empty($_SESSION['country'])){
$country =$_SESSION['country'];
}elseif(isset($_COOKIE['country']) && !empty($_COOKIE['country'])){
$country =$_COOKIE['country'];
}elseif($value = geoip_country_code_by_name(ip())){
$country = $value;
}else{
$country = $defaultCountry;
}
if(isset($content[$country])){
$country =$content[$country];
}else{
$country = $content[$defaultCountry];//Default ..
}
#2
1
First check if the country code is in the $content
array as a key or not, if not serve the first array as a default. To check if the key exists in array or not use array_key_exists().
首先检查$content数组中的国家代码是否是键,如果不将第一个数组作为默认值提供的话。要检查键在数组中是否存在,请使用array_key_exists()。
Like this,
像这样,
$countrycode="IN";
if(!array_key_exists($countrycode,$content)) {
$countryarray=$content[0];
} else {
$countryarray=$content[$countrycode];
}
Above code will return country's content if available or first if not found in an array.
如果可用,以上代码将返回国家的内容;如果不在数组中,则返回国家的内容。
#3
0
You can check it with ternary operator
also
你也可以用三元算符检查
countryArr = array();
$countryArr = array_key_exists($code,$content) ? $content[$code] : $content['GB'];