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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Custom code ( country detection) and displaying in pages

Started by admin0 3 years ago · 1 replies · 282 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 Jerry Hunt, 4 days ago
2 95 14 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago