How to Target Country Specific Visitors with Cloudflare

If you would like to target specific contents or ads for visitors in certain countries easily, you may use the code below. Make sue you change the array in the target countries.

Bear in mind that this code will only work if you are using Cloudflare service for your domain & enable IP Geolocation in the specific domain. IP Geolocation option can be enabled in Network section of Cloudflare panel. By enabling IP Geolocation, you may retrieve IP Geolocation information from the CF-IPCountry HTTP header.

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

$target_country = array('GB', 'US');

if(in_array($country_code, $target_country) {
// action if true
} else {
// action if false
} 

0 comments… add one

Leave a Comment