View Single Post
Old 11-20-2017, 11:50 AM  
vintageadult
Confirmed User
 
Industry Role:
Join Date: Jun 2015
Location: NL
Posts: 358
Quote:
Originally Posted by faperoni View Post
No need for ip2location if you are using Cloudflare. Just enable the "IP Geolocation" option and you can get the visitors country like this:

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

Now lets say you want to block visitors from Kuwait you simply do:

if($country_code == 'KW'){
exit; //or redirect or whatever
}

Multiple countries:

$blocked_countries = array('KW','AE','IQ');
if (in_array($country_code, $blocked_countries)) {
exit;//or redirect whatever
}

CF guide:
support.cloudflare[.]com/hc/en-us/articles/200168236-What-does-Cloudflare-IP-Geolocation-do-

Country codes:
github[.]com/lukes/ISO-3166-Countries-with-Regional-Codes/blob/master/all/all.csv

Let me know if you need help setting this up, good luck!
Where should I add this code:
Code:
if($country_code == 'KW'){
    exit; //or redirect or whatever
}
to .htaccess or where?
Does it works on shared hosting?
vintageadult is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote