Custom code ( country detection) and displaying in pages

Hi,

I have been using syfmony so far, but I recently found grav and I like it.

I have this requirement.

Whenever someone visits the website, I need to run a custom code that will detect his geolocation based on the ip address and I can save those data into variable/session. Then based on this, I need to pass down those in the pages via twig to show the corresponding variables based on the location detected.

I understand creating a new plugin to handle specific urls, but this one is to display things in current pages via twig or a custom code .

for example, my index.php ( or the main controller) could have a code that does a remote curl init and gets the $country_name as variable.

I would like to pass this $country_name in maybe session so that all pages have access to it.
I would also like to , based on this $country_name decide which pages gets shown to which countries.

Thanks,

I run my site through Cloudflare and they return geo-location on request. Then I just use something like this:

{% if grav.uri.getAllHeaders['Cf-Ipcontinent'] == 'OC' %}
      {% set antipodes = "on" %}
{% endif %}

I only need to check if the viewer is in Oceania or not - but the cloudflare data is far more granular if needed.