![]() |
How to block certain countries by using cloudflare's free version?
How do you block some middle east country traffic while using cloudflare's free package?
|
Quote:
but you can try add a IP range for a country for example : IP Address Ranges by Country | IP2Location LITE tnx. |
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! |
Quote:
And beside on that I have another problem with cloudflare: Whenever I adjust dns to cloudflare my wordpress site starts logging out me from backend. Every 10 or 20 seconds it keeps logging me out and asks for login again and also the website posts goes back to older times. I tried everthing: like purging cache , deleting other caching files plugins but it didnt fix this issue. |
Yes that feature is available on the free plan:
Cloudflare -> Network -> IP Geolocation Are you 100% sure that you are pointing cloudflare DNS to the right server? And do you have the same issues if you disable all CF features? (click the orange cloud icon on the right side of the dns entries, if the cloud icon is grey cloudflare will not cache or do anything with your traffic) |
Quote:
Code:
if($country_code == 'KW'){ Does it works on shared hosting? |
Best would be a php page that is included on all your pages. I see you use wordpress (from your sig) you could add that code to the wp_config.php file. Make a backup first.
So enable the cloudflare "IP Geolocation" setting and then add the code below anywhere between the <?php here ?> tags $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; if($country_code == 'KW'){ exit; //or redirect or whatever } |
All times are GMT -7. The time now is 09:09 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc