You can enable IP Geolocation to have Cloudflare geolocate visitors to your website and pass the country code to you in ISO 3166-1 Alpha 2 format. 


You will find the IP Geolocation option under the Network app in the Cloudflare dashboard.


The option for IP geolocation will be found near the bottom of the page:


Once enabled, Cloudflare adds a header called "CF-IPCountry" to all requests we make to your website. Here are a couple of examples of how to access/store this value:

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; // to access in PHP
$country_code = $ENV{"HTTP_CF_IPCOUNTRY"}; # to access in Perl


Cloudflare includes this information for both IPv4 and IPv6 addresses. Currently, the IPv4 information is more robust, but we expect the IPv6 data to improve rapidly.


XX means there is no country data. T1 is a non-standard country code used for the Tor network.