简体   繁体   English

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

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

I want a page to block EU countries except the UK, while allowing all other non-EU countries. 我想要一个页面来阻止除英国以外的欧盟国家,同时允许所有其他非欧盟国家/地区。 By block I mean I want another message, such as "Service Unavailable in your Country" appearing rather than the normal page content. 用块表示,我要显示其他消息,例如“您所在的国家/地区不可用的服务”,而不是正常的页面内容。 How can this be done? 如何才能做到这一点?

Note: I do not want to effect google-bot ranking by blocking these non-UK EU countries. 注意:我不想通过阻止这些非英国欧盟国家/地区来影响google-bot排名。

Edit: It's a VAT MOSS thing, nothing sinister. 编辑:这是增值税苔藓的东西,没有险恶。

You can use the geoplugin.net geo API. 您可以使用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      
            }

Edited the code a bit :) 编辑了一下代码:)

You'll need to check IP ranges and block those that are in the ranges you don't want. 您需要检查IP范围并阻止那些您不想要的范围。 Note that it's possible to get around these restrictions using a proxy or VPN or some such technology. 请注意,可以使用代理或VPN或某些类似技术来解决这些限制。

As Frank says, you will need to check the IP addresses against a geolocation database. 正如Frank所说,您将需要根据地理位置数据库检查IP地址。 An answered question on this topic already exists . 关于这个话题的答案已经存在

Below is a re-usable PHP function that uses 'ip-api.com' API to return location data on an IP and check it against a white-list of current EU member countries. 以下是可重复使用的PHP函数,该函数使用“ ip-api.com” API返回IP上的位置数据,并根据当前欧盟成员国的白名单对其进行检查。 The white-list is easy to maintain, which is good, as countries entering and leaving the EU are in a continual flux. 白名单易于维护,这很好,因为进出欧盟的国家不断变化。 The white-list was put together on July 24, 1018 with the data crossed-checked between the EU official website and Wikipedia. 白名单于1018年7月24日汇总在一起,数据经过欧盟官方网站和Wikipedia之间的交叉核对。 The 'ip-api.com' API is free for personal use (contact for commercial use), and can be run from your local server as well, and without registration or domain requirement. “ 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;
}

And to use the function... 并使用该功能...

if (inEU( $ip )){
  // IP address IS in an EU country
}
else {
  // IP address IS NOT in an EU country
}

This function also cross-checks the returned location data in the same loop, so if there is a typo in one var, it would still find the location using the other var. 此函数还会在同一循环中交叉检查返回的位置数据,因此,如果一个变量中有错别字,它将仍然使用另一个变量来查找位置。 For both to be wrong is not likely. 两者都不可能出错。

Unlike the 'geoplugin.net' API in Quadcore's example, which only checks on a continent value of 'EU', this function checks it against actual EU member countries. 与Quadcore的示例中的“ geoplugin.net” API不同,该API仅检查大洲的“ EU”值,而该函数则根据实际的欧盟成员国对其进行检查。

This function can also be easily be adapted to work with many other IP to location API's that return a JSON response, including the 'geoplugin.net' API in Quadcore's example. 也可以轻松地将此功能调整为与其他许多IP地址一起使用,以返回JSON响应的位置API,包括Quadcore示例中的“ geoplugin.net” API。 It can easily be adapted as an 'allow only' white-list, rather than 'ban' white-list. 它可以很容易地被修改为“仅允许”白名单,而不是“禁止”白名单。

Hope this helps! 希望这可以帮助!

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

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