How to get country code in every HTTP request by using cloudflare? Here is how:

First, you need to have already working setup with cloudflare, that means your DNS params should be already set and in place and your site is served through cloudflare.

 

Enable IP Geolocation

Now, when you are protected by cloudflare, you can use some extra features offered "out of the box" by cloudflare. One of them is getting information about country from where request is coming to your server.

To enable this feature go to your account in Cloudflare and select "Network" section. Under the networks section, be sure that you have "IP Geo location" set to on!

 

 

After this is set to on, every request will have additional HTTP header parameter: cf-ipcountry

which will contain country code of incoming request.

In #Laravel you can easily grab this param using  this line:

                    $request->header('cf-ipcountry');
                  

in plain PHP, you could use get_headers() function: