Projects running via Servd's Ingress Platform automatically include Cloudflare geolocation headers in inbound requests. These can be read by your project's code to geolocate your site's visitors.
You can verify these headers are coming through by visiting your project's PHP Info Utilities page in your Craft control panel. You should see the following headers (in SCREAMING_SNAKE_CASE format):
`CF-IPLatitude`
`CF-IPLongitude`
`CF-IPCity`
`CF-IPCountry`
`CF-Postal-Code`
`CF-Region-Code`
`CF-Region`
`CF-Continent`
`CF-Timezone`
`CF-ConnectingIP`
We'd recommend using these headers over the Geomate plugin, as it avoids you having to manually maintain a local geolocation database. In addition, Cloudflare's geolocation data is continually updated, so is more likely to be up-to-date and accurate.
Usage Examples
In twig:
{% set allHeaders = craft.app.request.getHeaders() %}
{% set userCountry = allHeaders['CF-IPCOUNTRY'] %}
In PHP:
$allHeaders = Craft::$app->request->getHeaders(); $userCountry = $allHeaders['CF-IPCOUNTRY'];
Use With Static Caching
Servd's Static Cache settings include an option to 'Vary cache based on request country of origin'. This functionality also uses the `CF-IPCountry` header value. You can therefore safely make use of the value of `CF-IPCountry` in your code, even with static caching enabled, if you have this option enabled.