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

PHP Code to Display Current Date & Time

Use the following code if you want to display Current Date and Time.

Simply copy the code in text file & save as .php file in the server. Change “Asia/Kuala_Lumpur” to your timezone.

   $current_date = date('d-m-Y : H:i:s');
   echo $current_date;