简体   繁体   中英

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.

Edit: It's a VAT MOSS thing, nothing sinister.

You can use the 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. Note that it's possible to get around these restrictions using a proxy or VPN or some such technology.

As Frank says, you will need to check the IP addresses against a geolocation database. 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. 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. 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.

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.

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. It can easily be adapted as an 'allow only' white-list, rather than 'ban' white-list.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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