Cloudfront IP location detection

Cloudfront provides a way to get the visitor’s IP location’s country using Maxmind geolocation tool:

Can this country_code = headers['cf-ipcountry'] value be stored as a global $_SERVER value in Wappler for use in workflows and AppConnect?

Hi Emiel,

Take a look at this:

You could create a Set Value country_code that would be accessible on every server action

Thanks for the prompt reply @Apple!

I don’t think I’m getting this right. Initially I wanted to set the value as a country_code $_SERVER variable:

image
However, I don’t know how to set the value then and moreover I think you are saying it’s better to set it as a global value with a Set value execution step, am I getting that right? If so, what would be the proper way to set the value according to CF’s instructions:

const country_code = headers['cf-ipcountry'];

Note that you’ll need to require http/https and instantiate the createServer() method

How can I instantiate the createServer() method in Wappler?

Hope you can help, thanks in advance!

Yes

Set Value global_country_code = {{ $_SERVER['cf-ipcountry'] }}

All headers are automatically inserted in $_SERVER variable by Wappler

The reason I said to use Set Value instead of directly accessing $_SERVER[‘cf-ipcountry’] is because in the future you might want to get away from Cloudfront, so it’s better not to hard-code your steps to use that specific header variable, and instead rely on the global_country_code we created, so that if you need to change you just need to change in one place

I’m using the Group step to create a group “Global”, and I’d put the Set Value country_code there

use $_SERVER.HTTP_CF_IPCOUNTRY

That just happened already :joy:

Just discovered that it’s also possible to get similar country codes from AWS CloudFront through the CloudFront-Viewer-Country header.

So I set the origin request policy for the distribution of my S3 buckets to include several headers, including the CloudFront-Viewer-Country one:

image

How can I set this value to a global variable in Wappler properly?
`Set Value global_country_code = {{ $_SERVER[‘CloudFront-Viewer-Country’] }} doesn’t seem to work…

I am also doubting if these headers are available at all. I assumed that calling at least one single image file (website logo e.g.) from the S3 bucket (distributed by CloudFront) would send the header along with the requested file, but is this actually true?

Thanks again for any help in advance!

You can do a Set Value server = {{ $_SERVER }} to see all existing variables