How would I detect all European countries with CloudFlare geolocation How would I detect all European countries with CloudFlare geolocation php php

How would I detect all European countries with CloudFlare geolocation


The value of $_SERVER["HTTP_CF_IPCOUNTRY"] is a country code. So first you will need a list of all the European country codes.

$europe = array('AD', 'AL', 'AT', 'AX', 'BA', 'BE', 'BG', 'BY', 'CH', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FO', 'FR', 'GB', 'GG', 'GI', 'GR', 'HR', 'HU', 'IE', 'IM', 'IS', 'IT', 'JE', 'LI', 'LT', 'LU', 'LV', 'MC', 'MD', 'ME', 'MK', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RS', 'RU', 'SE', 'SI', 'SJ', 'SK', 'SM', 'UA', 'VA');

You can then use this list to validate the request:

if(in_array($country_code, $europe)) {  // Europe} else {  // Not Europe}

If you want to target specific regions, you can use these lists:

$eastern = array('BG', 'BY', 'CZ', 'HU', 'MD', 'PL', 'RO', 'RU', 'SK', 'UA');$northern = array('AX', 'DK', 'EE', 'FI', 'FO', 'GB', 'GG', 'IE', 'IM', 'IS', 'JE', 'LT', 'LV', 'NO', 'SE', 'SJ');$southern = array('AD', 'AL', 'BA', 'ES', 'GI', 'GR', 'HR', 'IT', 'ME', 'MK', 'MT', 'PT', 'RS', 'SI', 'SM', 'VA');$western = array('AT', 'BE', 'CH', 'DE', 'FR', 'LI', 'LU', 'MC', 'NL');

For example:

if(in_array($country_code, $eastern)) {  // Eastern Europe} else {  // Not Eastern Europe}

I contacted CloudFlare to make sure the list of country codes are valid. They sent me a list of possible values for $_SERVER["HTTP_CF_IPCOUNTRY"]. I validated the above arrays, all country codes in those arrays are in the list of possible values.

For reference, here is the list of possible values:

A1  Anonymous ProxyA2  Satellite ProviderO1  Other CountryAD  AndorraAE  United Arab EmiratesAF  AfghanistanAG  Antigua and BarbudaAI  AnguillaAL  AlbaniaAM  ArmeniaAO  AngolaAP  Asia/Pacific RegionAQ  AntarcticaAR  ArgentinaAS  American SamoaAT  AustriaAU  AustraliaAW  ArubaAX  Aland IslandsAZ  AzerbaijanBA  Bosnia and HerzegovinaBB  BarbadosBD  BangladeshBE  BelgiumBF  Burkina FasoBG  BulgariaBH  BahrainBI  BurundiBJ  BeninBL  Saint BartelemeyBM  BermudaBN  Brunei DarussalamBO  BoliviaBQ  Bonaire, Saint Eustatius and SabaBR  BrazilBS  BahamasBT  BhutanBV  Bouvet IslandBW  BotswanaBY  BelarusBZ  BelizeCA  CanadaCC  Cocos (Keeling) IslandsCD  Congo, The Democratic Republic of theCF  Central African RepublicCG  CongoCH  SwitzerlandCI  Cote d'IvoireCK  Cook IslandsCL  ChileCM  CameroonCN  ChinaCO  ColombiaCR  Costa RicaCU  CubaCV  Cape VerdeCW  CuracaoCX  Christmas IslandCY  CyprusCZ  Czech RepublicDE  GermanyDJ  DjiboutiDK  DenmarkDM  DominicaDO  Dominican RepublicDZ  AlgeriaEC  EcuadorEE  EstoniaEG  EgyptEH  Western SaharaER  EritreaES  SpainET  EthiopiaEU  EuropeFI  FinlandFJ  FijiFK  Falkland Islands (Malvinas)FM  Micronesia, Federated States ofFO  Faroe IslandsFR  FranceGA  GabonGB  United KingdomGD  GrenadaGE  GeorgiaGF  French GuianaGG  GuernseyGH  GhanaGI  GibraltarGL  GreenlandGM  GambiaGN  GuineaGP  GuadeloupeGQ  Equatorial GuineaGR  GreeceGS  South Georgia and the South Sandwich IslandsGT  GuatemalaGU  GuamGW  Guinea-BissauGY  GuyanaHK  Hong KongHM  Heard Island and McDonald IslandsHN  HondurasHR  CroatiaHT  HaitiHU  HungaryID  IndonesiaIE  IrelandIL  IsraelIM  Isle of ManIN  IndiaIO  British Indian Ocean TerritoryIQ  IraqIR  Iran, Islamic Republic ofIS  IcelandIT  ItalyJE  JerseyJM  JamaicaJO  JordanJP  JapanKE  KenyaKG  KyrgyzstanKH  CambodiaKI  KiribatiKM  ComorosKN  Saint Kitts and NevisKP  Korea, Democratic People's Republic ofKR  Korea, Republic ofKW  KuwaitKY  Cayman IslandsKZ  KazakhstanLA  Lao People's Democratic RepublicLB  LebanonLC  Saint LuciaLI  LiechtensteinLK  Sri LankaLR  LiberiaLS  LesothoLT  LithuaniaLU  LuxembourgLV  LatviaLY  Libyan Arab JamahiriyaMA  MoroccoMC  MonacoMD  Moldova, Republic ofME  MontenegroMF  Saint MartinMG  MadagascarMH  Marshall IslandsMK  MacedoniaML  MaliMM  MyanmarMN  MongoliaMO  MacaoMP  Northern Mariana IslandsMQ  MartiniqueMR  MauritaniaMS  MontserratMT  MaltaMU  MauritiusMV  MaldivesMW  MalawiMX  MexicoMY  MalaysiaMZ  MozambiqueNA  NamibiaNC  New CaledoniaNE  NigerNF  Norfolk IslandNG  NigeriaNI  NicaraguaNL  NetherlandsNO  NorwayNP  NepalNR  NauruNU  NiueNZ  New ZealandOM  OmanPA  PanamaPE  PeruPF  French PolynesiaPG  Papua New GuineaPH  PhilippinesPK  PakistanPL  PolandPM  Saint Pierre and MiquelonPN  PitcairnPR  Puerto RicoPS  Palestinian TerritoryPT  PortugalPW  PalauPY  ParaguayQA  QatarRE  ReunionRO  RomaniaRS  SerbiaRU  Russian FederationRW  RwandaSA  Saudi ArabiaSB  Solomon IslandsSC  SeychellesSD  SudanSE  SwedenSG  SingaporeSH  Saint HelenaSI  SloveniaSJ  Svalbard and Jan MayenSK  SlovakiaSL  Sierra LeoneSM  San MarinoSN  SenegalSO  SomaliaSR  SurinameSS  South SudanST  Sao Tome and PrincipeSV  El SalvadorSX  Sint MaartenSY  Syrian Arab RepublicSZ  SwazilandTC  Turks and Caicos IslandsTD  ChadTF  French Southern TerritoriesTG  TogoTH  ThailandTJ  TajikistanTK  TokelauTL  Timor-LesteTM  TurkmenistanTN  TunisiaTO  TongaTR  TurkeyTT  Trinidad and TobagoTV  TuvaluTW  TaiwanTZ  Tanzania, United Republic ofUA  UkraineUG  UgandaUM  United States Minor Outlying IslandsUS  United StatesUY  UruguayUZ  UzbekistanVA  Holy See (Vatican City State)VC  Saint Vincent and the GrenadinesVE  VenezuelaVG  Virgin Islands, BritishVI  Virgin Islands, U.S.VN  VietnamVU  VanuatuWF  Wallis and FutunaWS  SamoaYE  YemenYT  MayotteZA  South AfricaZM  ZambiaZW  Zimbabwe


Look at Configuring Cloudflare IP Geolocation

Cloudflare can include the country code of the visitor's IP (in ISO 3166-1 Alpha 2 format) with each request between Cloudflare and the upstream origin web server.

You can access full list at wikipedia linked page.

AD  AndorraAE  United Arab EmiratesAF  AfghanistanAG  Antigua and BarbudaAI  AnguillaAL  AlbaniaAM  ArmeniaAO  AngolaAQ  AntarcticaAR  ArgentinaAS  American SamoaAT  AustriaAU  AustraliaAW  ArubaAX  Åland IslandsAZ  AzerbaijanBA  Bosnia and HerzegovinaBB  BarbadosBD  BangladeshBE  BelgiumBF  Burkina FasoBG  BulgariaBH  BahrainBI  BurundiBJ  BeninBL  Saint BarthélemyBM  BermudaBN  Brunei DarussalamBO  Bolivia (Plurinational State of)BQ  Bonaire, Sint Eustatius and SabaBR  BrazilBS  BahamasBT  BhutanBV  Bouvet IslandBW  BotswanaBY  BelarusBZ  BelizeCA  CanadaCC  Cocos (Keeling) IslandsCD  Congo, Democratic Republic of theCF  Central African RepublicCG  CongoCH  SwitzerlandCI  Côte d'IvoireCK  Cook IslandsCL  ChileCM  CameroonCN  ChinaCO  ColombiaCR  Costa RicaCU  CubaCV  Cabo VerdeCW  CuraçaoCX  Christmas IslandCY  CyprusCZ  CzechiaDE  GermanyDJ  DjiboutiDK  DenmarkDM  DominicaDO  Dominican RepublicDZ  AlgeriaEC  EcuadorEE  EstoniaEG  EgyptEH  Western SaharaER  EritreaES  SpainET  EthiopiaFI  FinlandFJ  FijiFK  Falkland Islands (Malvinas)FM  Micronesia (Federated States of)FO  Faroe IslandsFR  FranceGA  GabonGB  United KingdomGD  GrenadaGE  GeorgiaGF  French GuianaGG  GuernseyGH  GhanaGI  GibraltarGL  GreenlandGM  GambiaGN  GuineaGP  GuadeloupeGQ  Equatorial GuineaGR  GreeceGS  South Georgia and the South Sandwich IslandsGT  GuatemalaGU  GuamGW  Guinea-BissauGY  GuyanaHK  Hong KongHM  Heard Island and McDonald IslandsHN  HondurasHR  CroatiaHT  HaitiHU  HungaryID  IndonesiaIE  IrelandIL  IsraelIM  Isle of ManIN  IndiaIO  British Indian Ocean TerritoryIQ  IraqIR  Iran (Islamic Republic of)IS  IcelandIT  ItalyJE  JerseyJM  JamaicaJO  JordanJP  JapanKE  KenyaKG  KyrgyzstanKH  CambodiaKI  KiribatiKM  ComorosKN  Saint Kitts and NevisKP  Korea (Democratic People's Republic of)KR  Korea, Republic ofKW  KuwaitKY  Cayman IslandsKZ  KazakhstanLA  Lao People's Democratic RepublicLB  LebanonLC  Saint LuciaLI  LiechtensteinLK  Sri LankaLR  LiberiaLS  LesothoLT  LithuaniaLU  LuxembourgLV  LatviaLY  LibyaMA  MoroccoMC  MonacoMD  Moldova, Republic ofME  MontenegroMF  Saint Martin (French part)MG  MadagascarMH  Marshall IslandsMK  North MacedoniaML  MaliMM  MyanmarMN  MongoliaMO  MacaoMP  Northern Mariana IslandsMQ  MartiniqueMR  MauritaniaMS  MontserratMT  MaltaMU  MauritiusMV  MaldivesMW  MalawiMX  MexicoMY  MalaysiaMZ  MozambiqueNA  NamibiaNC  New CaledoniaNE  NigerNF  Norfolk IslandNG  NigeriaNI  NicaraguaNL  NetherlandsNO  NorwayNP  NepalNR  NauruNU  NiueNZ  New ZealandOM  OmanPA  PanamaPE  PeruPF  French PolynesiaPG  Papua New GuineaPH  PhilippinesPK  PakistanPL  PolandPM  Saint Pierre and MiquelonPN  PitcairnPR  Puerto RicoPS  Palestine, State ofPT  PortugalPW  PalauPY  ParaguayQA  QatarRE  RéunionRO  RomaniaRS  SerbiaRU  Russian FederationRW  RwandaSA  Saudi ArabiaSB  Solomon IslandsSC  SeychellesSD  SudanSE  SwedenSG  SingaporeSH  Saint Helena, Ascension and Tristan da CunhaSI  SloveniaSJ  Svalbard and Jan MayenSK  SlovakiaSL  Sierra LeoneSM  San MarinoSN  SenegalSO  SomaliaSR  SurinameSS  South SudanST  Sao Tome and PrincipeSV  El SalvadorSX  Sint Maarten (Dutch part)SY  Syrian Arab RepublicSZ  EswatiniTC  Turks and Caicos IslandsTD  ChadTF  French Southern TerritoriesTG  TogoTH  ThailandTJ  TajikistanTK  TokelauTL  Timor-LesteTM  TurkmenistanTN  TunisiaTO  TongaTR  TurkeyTT  Trinidad and TobagoTV  TuvaluTW  Taiwan, Province of ChinaTZ  Tanzania, United Republic ofUA  UkraineUG  UgandaUM  United States Minor Outlying IslandsUS  United States of AmericaUY  UruguayUZ  UzbekistanVA  Holy SeeVC  Saint Vincent and the GrenadinesVE  Venezuela (Bolivarian Republic of)VG  Virgin Islands (British)VI  Virgin Islands (U.S.)VN  Viet NamVU  VanuatuWF  Wallis and FutunaWS  SamoaYE  YemenYT  MayotteZA  South AfricaZM  ZambiaZW  Zimbabwe

Then cloudflare never use "Anonymous Proxy", "Satellite Provider", "Other Country"