Skip to content Skip to sidebar Skip to footer

Restricting content for visitors from certain countries

Geo-targeting

There are many visitors who are always different on a website. Every visitor is certainly not from the same country, sometimes there are visitors from other countries who also go to the website. This is due to the fact that every visitor at their location has a different IP address that can be read out directly, provided that no proxy is used. Basically the original IP can be direct recognition of the country / location layout (GEO). However, if a proxy is used, the actual layout cannot be recognized.

This time I’ll share how to do it Limit content to visitors from certain countries which of course is simple and requires the help of PHP. This method can also be used to run ads for specific countries for better results. Have you ever seen a website that restricts registration access to certain countries? So this can also be done in a simple script that I’ll be sharing.
Please note a small code below.

The above code can be practiced directly and for the country retrieval use another website, namely ipinfo.io. In the functional area trim() in string $ from used to remove spaces. To limit the country usage function otherwise and the country code of the form ISO 3166-1 what is to be understood here

The above code can be customized by yourself on the page or content you want to restrict to a specific country. Make sure it works before using it file_get_contents () was enabled in the PHP settings as getting the country from the IP uses the help of other sites as mentioned above. Or if you want to use cURL please copy the code below.

The above code is a replacement for file_get_contents which can make code run faster. Using cURL is actually better than file_get_contents as cURL gives more options to make website content more perfect.
If you have any questions about any of the above, please comment. : Mr. Green:

Hopefully useful and good luck