AI & Tech

Geoblocking 101: My Journey to Cleaner Web Traffic


Geoblocking 101: My Journey to Cleaner Web Traffic

Read

Applied

Mastered


As a website owner, I recently dove into my Google Analytics data and noticed something concerning - a significant number of visits were coming from overseas. While international traffic can be great, I knew that certain countries are unfortunately more associated with spam and low-quality visits. This realization led me on a journey to implement a country-based blocking system to help maintain the quality of traffic to my site.

The Challenge

My goal was clear: I needed a way to block IP addresses from specific countries that I identified as potential sources of spam or low-quality traffic. But how could I determine a visitor's country based on their IP address?

Enter ipinfo.io

After some research, I discovered ipinfo.io, a service that provides IP geolocation data. Even better, they offer a free tier that would suit my needs perfectly. With this API, I could resolve IP addresses to country codes, giving me the information I needed to implement my blocking system.

Crafting the Solution

With the API chosen, I needed a script that could:

  1. Get the visitor's IP address
  2. Query the ipinfo.io API to determine the country
  3. Check if the country is on my block list
  4. Deny access if necessary

AI for the Script

		function blockCountries($blockedCountries, $apiToken) {
			$userIP = $_SERVER['REMOTE_ADDR'];
			$url = "http://ipinfo.io/{$userIP}/json?token={$apiToken}";
			$userInfo = json_decode(file_get_contents($url));
			
			if (isset($userInfo->country) && in_array($userInfo->country, $blockedCountries)) {
				die("Access denied from your country.");}
		}

		// Usage example:
		$blockedCountries = ['RU', 'CN', 'KP'];
		$apiToken = 'your_ipinfo_token_here';
		blockCountries($blockedCountries, $apiToken);
		

Let's break down how this script works

  1. The function `blockCountries` takes two parameters: an array of country codes to block, and your ipinfo.io API token.
  2. It gets the visitor's IP address using `$_SERVER['REMOTE_ADDR']`.
  3. It constructs a URL to query the ipinfo.io API, including the IP address and your API token.
  4. The API response (in JSON format) is decoded into a PHP object.
  5. If the API successfully returned a country code, the script checks if it's in the list of blocked countries.
  6. If the visitor's country is in the blocked list, the script terminates with an "Access denied" message.

Implementation and Results

Implementing this script was straightforward. I simply included it at the top of my PHP pages, making sure to run it before any output is sent to the browser.

The results didn't take long to resolve. The number of visits from blocked countries dropped significantly, and my analytics began showing a more accurate picture of my genuine traffic.

Considerations and Future Improvements

While this solution has been effective, there are a few things to keep in mind

  1. API Limits: The free tier of ipinfo.io has usage limits. For high-traffic sites, you might need to consider a paid plan or implement caching.
  2. False Positives: Be cautious about which countries you block. You might unintentionally block legitimate users.
  3. VPNs and Proxies: Determined individuals can still bypass this system using VPNs or proxies.
  4. User Experience: In the future, I might implement a more informative blocking page, perhaps explaining why access is restricted and providing alternatives for legitimate users.

Implementing this country-based blocking system has significantly improved the quality of traffic to my site. It's a simple yet effective way to reduce spam and low-quality visits, allowing me to focus on genuine engagement from my target audience.

Remember, while this method can be quite effective, it should be part of a broader strategy for managing site traffic and security. Always monitor your analytics and be ready to adjust your approach as needed.

Happy coding, and here's to cleaner, more meaningful web traffic!


Share this post