Using IP2Proxy Library in Drupal

This tutorial shows you about how to deploy IP2Proxy Library into Drupal 8 and 9, and use it with the IP2Proxy BIN database.

Step 1: Navigate to the root folder of your existing Drupal project.

Step 2: Install IP2Proxy library using Composer.
composer require ip2location/ip2proxy-php

Step 3: Download the free IP2Proxy LITE database or IP2Proxy Commercial database and upload the BIN file to vendor/ip2location/ip2proxy-php/data/ directory.

IP2Location LITE database

Step 4: Open an existing module or controller in Drupal project.

Step 5: Add IP2Proxy lookup by referring example below:

<?php 
	namespace Drupal\example_module\Controller; 
	
	class ExampleController { 
		public function example() { 
			$db = new \IP2Proxy\Database('../vendor/ip2location/ip2proxy-php/data/PX3.BIN', \IP2Proxy\Database::FILE_IO); 
			$result = $db->lookup($_SERVER['REMOTE_ADDR']); 
			
			return [ '#markup' => 'Welcome, your are ' . (($result['isProxy'] == 'Y') ? '' : 'not ') . 'using proxy server to visit my website.', ]; 
		} 
	}

Step 6: To display other fields and variables, please refer to IP2Proxy PHP page.

Was this article helpful?

Related Articles