Create a template conditional based off Cloudflare GeoIP Header?

Brent W

Well-known member
Cloudflare seems to include a header with every request that holds the visitors location. Would it be possible to create a template conditional based off this header?

What is IP Geolocation?
CloudFlare can geolocate visitors to your website and pass the country code on to you. Once enabled, we will then add an HTTP header named "CF-IPCountry" to all requests we make to your website.

Note: CloudFlare includes this information for both IPv4 and IPv6 addresses.

Would make it easy to target advertisements to specific countries without the use of robust ad server programs or additional queries to 3rd party services.

Here is the code example. Looks pretty straight forward.

PHP:
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
 
Last edited:
There's a visitor_setup code event. If you utilise this, the variable will be part of the visitor object.

It's not saved anywhere, it just makes the variable available in code and templates using the current $visitor / XenForo_Visitor object.
 
Hi Brent, did you found a solution for this?
Via Template edit, as I don't know how to extend XenForo Code.

Thanks!
 
Top Bottom