繁体   English   中英

PhP-如何封锁除英国以外的所有欧盟国家,并允许所有非欧盟国家?

[英]PhP - How do I block every EU country except the UK, and allow all non EU countries?

我想要一个页面来阻止除英国以外的欧盟国家,同时允许所有其他非欧盟国家/地区。 用块表示,我要显示其他消息,例如“您所在的国家/地区不可用的服务”,而不是正常的页面内容。 如何才能做到这一点?

注意:我不想通过阻止这些非英国欧盟国家/地区来影响google-bot排名。

编辑:这是增值税苔藓的东西,没有险恶。

您可以使用geoplugin.net geo 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      
            }

编辑了一下代码:)

您需要检查IP范围并阻止那些您不想要的范围。 请注意,可以使用代理或VPN或某些类似技术来解决这些限制。

正如Frank所说,您将需要根据地理位置数据库检查IP地址。 关于这个话题的答案已经存在

以下是可重复使用的PHP函数,该函数使用“ ip-api.com” API返回IP上的位置数据,并根据当前欧盟成员国的白名单对其进行检查。 白名单易于维护,这很好,因为进出欧盟的国家不断变化。 白名单于1018年7月24日汇总在一起,数据经过欧盟官方网站和Wikipedia之间的交叉核对。 “ 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:&nbsp;<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
}

此函数还会在同一循环中交叉检查返回的位置数据,因此,如果一个变量中有错别字,它将仍然使用另一个变量来查找位置。 两者都不可能出错。

与Quadcore的示例中的“ geoplugin.net” API不同,该API仅检查大洲的“ EU”值,而该函数则根据实际的欧盟成员国对其进行检查。

也可以轻松地将此功能调整为与其他许多IP地址一起使用,以返回JSON响应的位置API,包括Quadcore示例中的“ geoplugin.net” API。 它可以很容易地被修改为“仅允许”白名单,而不是“禁止”白名单。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM