PhP - 我如何阻止除英国以外的所有欧盟国家,并允许所有非欧盟国家?
PhP - How do I block every EU country except the UK, and allow all non EU countries?
我想要一个页面来阻止除英国以外的欧盟国家,同时允许所有其他非欧盟国家。我所说的块是指我想要另一条消息,例如 "Service Unavailable in your Country" 出现而不是正常的页面内容。如何做到这一点?
注意:我不想通过屏蔽这些非英国欧盟国家来影响 google-bot 排名。
编辑:这是 VAT MOSS 的东西,没什么险恶的。
您需要检查 IP ranges 并阻止那些在您不想要的范围内的内容。请注意,可以使用代理或 VPN 或某些此类技术来绕过这些限制。
正如 Frank 所说,您需要根据地理位置数据库检查 IP 地址。关于此主题的已回答问题 already exists。
您可以使用 geoplugin.net 地理位置 API。
//Get user IP address
$ip=$_SERVER['REMOTE_ADDR'];
//Using the API to get information about this IP
$details = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=$ip"));
//Using the geoplugin to get the continent for this IP
$continent=$details->geoplugin_continentCode;
//And for the country
$country=$details->geoplugin_countryCode;
//If continent is Europe
if($continent==="EU" && $country==="UK" || $continent!="EU"){
//Do action if country is UK or not from Europe
}else{
//Do action if country is in Europe , but its not UK
}
稍微编辑了代码:)
下面是一个可重复使用的 PHP 函数,它使用 'ip-api.com' API 到 IP 上的 return 位置数据,并根据当前的白名单检查它欧盟成员国。白名单易于维护,这很好,因为进入和离开欧盟的国家不断变化。白名单于 1018 年 7 月 24 日汇总,数据在欧盟官方网站和维基百科之间进行了交叉核对。 'ip-api.com' API 个人使用免费(商业用途请联系),也可以从您的本地服务器 运行 获取,无需注册或域要求。
function inEU($ip_input){
// Validate the IP address
if (filter_var($ip_input, FILTER_VALIDATE_IP) === false){
// Not a valid IP address - build error response
$message_string =
'<div style="width:100%; margin-top:50px; text-align:center;">'.
'<div style="width:100%; font-family:arial,sans-serif; font-size:24px; color:#c00; centered">'.
'ERROR: <span style="color:#fd0">Invalid IP Address</span>'.
'</div>'.
'</div>';
echo $message_string;
exit;
}
// Array of country names and country codes of European Union member countries
$eu_members = array(
'Austria','AT', 'Belgium','BE', 'Bulgaria','BG',
'Croatia','HR', 'Cyprus','CY', 'Czech Republic','CZ',
'Denmark','DK', 'Estonia','EE', 'Finland','FI',
'France','FR', 'Germany','DE', 'Greece','GR',
'Hungary','HU', 'Ireland','IE', 'Italy','IT',
'Latvia','LV', 'Lithuania','LT', 'Luxembourg','LU',
'Malta','MT', 'Netherlands','NL', 'Netherlands Antilles','AN',
'Poland','PL', 'Portugal','PT', 'Romania','RO',
'Slovakia','SK', 'Slovenia','SI', 'Spain','ES',
'Sweden','SE', 'United Kingdom','GB','UK'
);
$query_url = 'http://ip-api.com/json/'.$ip_input; // Build query URL for IP to JSON Data request
$ip_data_fetched = file_get_contents($query_url); // Return IP Data JSON as a string
$ip_data_fetched = utf8_encode($ip_data_fetched); // Encode returned JSON string to utf-8 if needed
$ip_data = json_decode($ip_data_fetched); // Decode utf-8 JSON string as PHP object
// Get the Country and Country Code for the IP from the returned data
$country = $ip_data->country; // Country Name (i.e; 'United States')
$countryCode = $ip_data->countryCode; // Country Code (i.e; 'US')
// Check the EU members array for match to either country or country code
$in_EU = false; // Ref for function boolean value returned - set false
$num_members = count($eu_members); // Number of indexes in EU members array (not # of members)
for ($i = 0; $i < $num_members; $i++){
$lc_eu_members = strtolower($eu_members[$i]);
if ($lc_eu_members === strtolower($country) || $lc_eu_members === strtolower($countryCode)){
$in_EU = true;
break;
}
}
return $in_EU;
}
并使用函数...
if (inEU( $ip )){
// IP address IS in an EU country
}
else {
// IP address IS NOT in an EU country
}
此函数还在同一循环中交叉检查 returned 位置数据,因此如果一个变量中有拼写错误,它仍会使用另一个变量查找位置。两者都错的可能性不大。
与 Quadcore 示例中的 'geoplugin.net' API 不同,它只检查大陆值 'EU',此函数根据实际的欧盟成员国检查它。
此功能也可以很容易地与许多其他 IP 一起工作,以定位 API 的 return JSON 响应,包括 'geoplugin.net' API 在 Quadcore 的例子中。它可以很容易地调整为 'allow only' 白名单,而不是 'ban' 白名单。
希望对您有所帮助!
我想要一个页面来阻止除英国以外的欧盟国家,同时允许所有其他非欧盟国家。我所说的块是指我想要另一条消息,例如 "Service Unavailable in your Country" 出现而不是正常的页面内容。如何做到这一点?
注意:我不想通过屏蔽这些非英国欧盟国家来影响 google-bot 排名。
编辑:这是 VAT MOSS 的东西,没什么险恶的。
您需要检查 IP ranges 并阻止那些在您不想要的范围内的内容。请注意,可以使用代理或 VPN 或某些此类技术来绕过这些限制。
正如 Frank 所说,您需要根据地理位置数据库检查 IP 地址。关于此主题的已回答问题 already exists。
您可以使用 geoplugin.net 地理位置 API。
//Get user IP address
$ip=$_SERVER['REMOTE_ADDR'];
//Using the API to get information about this IP
$details = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=$ip"));
//Using the geoplugin to get the continent for this IP
$continent=$details->geoplugin_continentCode;
//And for the country
$country=$details->geoplugin_countryCode;
//If continent is Europe
if($continent==="EU" && $country==="UK" || $continent!="EU"){
//Do action if country is UK or not from Europe
}else{
//Do action if country is in Europe , but its not UK
}
稍微编辑了代码:)
下面是一个可重复使用的 PHP 函数,它使用 'ip-api.com' API 到 IP 上的 return 位置数据,并根据当前的白名单检查它欧盟成员国。白名单易于维护,这很好,因为进入和离开欧盟的国家不断变化。白名单于 1018 年 7 月 24 日汇总,数据在欧盟官方网站和维基百科之间进行了交叉核对。 'ip-api.com' API 个人使用免费(商业用途请联系),也可以从您的本地服务器 运行 获取,无需注册或域要求。
function inEU($ip_input){
// Validate the IP address
if (filter_var($ip_input, FILTER_VALIDATE_IP) === false){
// Not a valid IP address - build error response
$message_string =
'<div style="width:100%; margin-top:50px; text-align:center;">'.
'<div style="width:100%; font-family:arial,sans-serif; font-size:24px; color:#c00; centered">'.
'ERROR: <span style="color:#fd0">Invalid IP Address</span>'.
'</div>'.
'</div>';
echo $message_string;
exit;
}
// Array of country names and country codes of European Union member countries
$eu_members = array(
'Austria','AT', 'Belgium','BE', 'Bulgaria','BG',
'Croatia','HR', 'Cyprus','CY', 'Czech Republic','CZ',
'Denmark','DK', 'Estonia','EE', 'Finland','FI',
'France','FR', 'Germany','DE', 'Greece','GR',
'Hungary','HU', 'Ireland','IE', 'Italy','IT',
'Latvia','LV', 'Lithuania','LT', 'Luxembourg','LU',
'Malta','MT', 'Netherlands','NL', 'Netherlands Antilles','AN',
'Poland','PL', 'Portugal','PT', 'Romania','RO',
'Slovakia','SK', 'Slovenia','SI', 'Spain','ES',
'Sweden','SE', 'United Kingdom','GB','UK'
);
$query_url = 'http://ip-api.com/json/'.$ip_input; // Build query URL for IP to JSON Data request
$ip_data_fetched = file_get_contents($query_url); // Return IP Data JSON as a string
$ip_data_fetched = utf8_encode($ip_data_fetched); // Encode returned JSON string to utf-8 if needed
$ip_data = json_decode($ip_data_fetched); // Decode utf-8 JSON string as PHP object
// Get the Country and Country Code for the IP from the returned data
$country = $ip_data->country; // Country Name (i.e; 'United States')
$countryCode = $ip_data->countryCode; // Country Code (i.e; 'US')
// Check the EU members array for match to either country or country code
$in_EU = false; // Ref for function boolean value returned - set false
$num_members = count($eu_members); // Number of indexes in EU members array (not # of members)
for ($i = 0; $i < $num_members; $i++){
$lc_eu_members = strtolower($eu_members[$i]);
if ($lc_eu_members === strtolower($country) || $lc_eu_members === strtolower($countryCode)){
$in_EU = true;
break;
}
}
return $in_EU;
}
并使用函数...
if (inEU( $ip )){
// IP address IS in an EU country
}
else {
// IP address IS NOT in an EU country
}
此函数还在同一循环中交叉检查 returned 位置数据,因此如果一个变量中有拼写错误,它仍会使用另一个变量查找位置。两者都错的可能性不大。
与 Quadcore 示例中的 'geoplugin.net' API 不同,它只检查大陆值 'EU',此函数根据实际的欧盟成员国检查它。
此功能也可以很容易地与许多其他 IP 一起工作,以定位 API 的 return JSON 响应,包括 'geoplugin.net' API 在 Quadcore 的例子中。它可以很容易地调整为 'allow only' 白名单,而不是 'ban' 白名单。
希望对您有所帮助!