Using GEOIP Joomla Plugin to Block IPS from...

Search
Other Pre-Sale questions
Forum

Using GEOIP Joomla Plugin to Block IPS from Other Countries

Paul's Avatar Paul
I want to use GEOIP in Joomla to block IPS from all countries except US from accessing site .
I have tried using php code to call the API and several other suggestions nothing seems to work, I have installed the plugins inside Joomla but there are
no options to block IPS . What am I missing ? Are these only features in paid version?
Peter van Westen's Avatar Peter van Westen ADMIN
The GeoIP library is used by a number of my other extensions to handle Geo Location based conditions.
If you want to use it in your custom PHP, then you can do that.
The GeoIP library can return the location data to you.
There is no auto-blocking feature in the library. All it does is return the location information.

But what have you tried? What is your custom PHP code?
Please post a rating at the Joomla! Extensions Directory
Paul's Avatar Paul
<?php
defined('_JEXEC') or die;

use Joomla\CMS\Plugin\CMSPlugin;
use GeoIp2\Database\Reader;

class plgSystemUSOnly extends CMSPlugin
{
    public function onAfterInitialise()
    {
        // Enable error reporting
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);

        // Path to the GeoLite2 database file
        $databasePath = ('database.mmdb');

        // Get user's IP address
        $userIp = $_SERVER['REMOTE_ADDR'];

        try {
            // Create a Reader object
            $reader = new Reader($databasePath);

            // Look up the user's IP address
            $record = $reader->city($userIp);

            // Check if the country is 'US' or in Europe
            $allowedCountries = ['US', 'GB', 'DE', 'FR', 'IT', 'ES', 'PL', 'RO', 'NL', 'BE', 'GR', 'CZ', 'PT', 'HU', 'SE', 'AT', 'BG', 'DK', 'FI', 'SK', 'IE', 'HR', 'LT', 'SI', 'LV', 'EE', 'CY', 'LU', 'MT', 'IS', 'NO', 'CH', 'LI'];
            if (!in_array($record->country->isoCode, $allowedCountries)) {
                // Redirect to the blocked page
                JFactory::getApplication()->redirect('/blocked.php');
            } else {
                // Redirect to the index page
                JFactory::getApplication()->redirect('/index.php');
            }
        } catch (\GeoIp2\Exception\AddressNotFoundException $e) {
            // The IP address is not in the database.
            die('IP address not found in database: ' . $e->getMessage());
        } catch (\Exception $e) {
            // Handle general exceptions
            die('An error occurred: ' . $e->getMessage());
        }
    }
}

// Display the loading page
echo <<<HTML
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            background-color: lightblue;
            font-family: Arial, sans-serif;
        }
        .loading-bar {
            position: relative;
            height: 20px;
            width: 100%;
            background-color: #f3f3f3;
        }
        .loading-bar:before {
            content: "";
            display: block;
            position: absolute;
            top: 0;
            left: 0;
            width: 0;
            height: 100%;
            background-color: #3498db;
            animation: loading 2s linear infinite;
        }
        @keyframes loading {
            from { width: 0; }
            to { width: 100%; }
        }
    </style>
</head>
<body>
    <h1>Checking for Malicious Uses and Loading...</h1>
    <div class="loading-bar"></div>
</body>
</html>
HTML;
?>


And...It just returns a blank page/
Paul's Avatar Paul
All I want to do is restrict my site to North American and European visitors as it is not relevant in other countries . This should not be that complicated but apparently its is . Can you sugggest a code or plugin that will work?
Peter van Westen's Avatar Peter van Westen ADMIN
It is beyond my support to debug your full php code.

But start with something like this:
use RegularLabs\Library\GeoIp\GeoIp;

...

$geoIP = new GeoIp();
$countryCode = $geoIP->countryCode;
Please post a rating at the Joomla! Extensions Directory
You can only post on this forum if you log in