Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Custom code ( country detection) and displaying in pages

Started by admin0 3 years ago · 1 replies · 381 views
3 years ago

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,

3 years ago

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

TWIG
{% 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.

Suggested topics

Topic Participants Replies Views Activity
General · by jean-louis67, 3 weeks ago
2 306 3 weeks ago
General · by Andy Miller, 3 weeks ago
0 310 3 weeks ago
General · by Veehem, 3 weeks ago
0 263 3 weeks ago
General · by milkboy, 4 weeks ago
0 264 4 weeks ago
General · by ian russell, 1 month ago
2 335 1 month ago