簡體   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