Block Selected IPs From Specific Pages In Joomla

One may need to block an IP address from all or specific pages for a variety of reasons, not necessarily related to malicious activity. This can be done within Joomla simply, and without hacking any core files.

Create A Plugin For The Job

A System plugin will serve best for the task because it is loaded globally, unless instructed otherwise.

1. Create the folder which will contain all the files and give it a relative name
the_ip_control

2. Create the primary function file within the folder and keep the name similar to the folder for simplicity
the_ip_control.php

<?php defined('_JEXEC') or die();
/**
* @package      plg_system_the_ip_control
* @copyright   Copyright (C) AnySiteYouWantHere.com All rights reserved.
* @license      GNU General Public License version 2 or later; see http://www.gnu.org/licenses/
**/

class plgSystemThe_IP_Control extends JPlugin {

   public function onAfterInitialise() {
      //do not render the function in admin area
      if(JFactory::getApplication()->isAdmin()){
         return ;
      }
      
      // define variable for use
      $currenturl   = JURI::getInstance()->toString();
      $getip       = $_SERVER['REMOTE_ADDR'];
      // get data entered in the IP text area
      $iplist    = explode( PHP_EOL, $this->params->get('ipblock') );
      // get data entered in the URL list text area
      $pages       = str_replace( PHP_EOL, '|', $this->params->get('pages') );

      // check data exist in the IP list text area and run
      if( !is_null($this->params->get('ipblock')) ) {
         // check what option the radio button is set on. either Block or Allow
         if( ($this->params->get('method') == 'block' && in_array($getip, $iplist)) ||
            ($this->params->get('method') == 'allow' && !in_array($getip, $iplist)) ) {
               if( preg_match('/'.$pages.'/', $currenturl) ) {
                  // send to the entered URL if all conditions are inline
                  header( 'Location: '.$this->params->get('redirect') );
                  exit();
               }
         }
      }

   
   }
   
   
}

3. Create the installer XML file within the folder and give it the same name as the function file
the_ip_control.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.7.0" type="plugin" group="system" method="upgrade">
   <name>System - The IP Control</name>
   <author>AnyWebSiteHere.com</author>
   <creationDate>06.15.2017</creationDate>
   <copyright>All Rights Reserved.</copyright>
   <license>license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html</license>
   <authorEmail>yourmail@yoursite.com</authorEmail>
   <authorUrl>http://www.yoursitehere.com</authorUrl>
   <version>1.0</version>
   <description>Allow or Deny IP</description>
   
   <files>
      <filename plugin="the_ip_control">the_ip_control.php</filename>
      <filename>index.html</filename>
   </files>
   
<config>
   <fields name="params">
      <fieldset name="basic">
         <field name="method" type="radio" class="btn-group btn-group-yesno" default="block" label="Method">
            <option value="block">Block</option>
            <option value="allow">Allow</option>
         </field>
         <field type="spacer" label="Add IP addresses. 1 per line" />
         <field name="ipblock" type="textarea" rows="5" class="span8 monospace" label="IP Block List" />
         <field name="redirect" type="text" hint="http://google.com" required="required" label="URL To Redirect" />
         <field type="spacer" label="Add matching URLs to allow or block. Can be all or part of a URL. 1 per line. Leave empty to affect the entire website" />
         <field name="pages" type="textarea" rows="5" class="span8 monospace" label="URLs" />
      </fieldset>
   </fields>
</config>

</extension>

Compress the directory with the files as a .zip and install at your Joomla website
Go to Extensions > Plugins and use the filter type System
Enable the plugin and enter some data. Try your own IP to confirm functionality

The resulting options page

This configuration will allow only users coming in via the included IPs to view pages with url parts matching the strings shown.

ip block plugin

This configuration will block only users coming in via the included IPs from viewing pages with url parts matching the strings shown.

ip block plug in

 


Sunday, 12 February 2017 10:19  |   137 views

 

Copyright © 2017 CMSEnergizer.com. All Rights Reserved.
This website is powered by the Joomla!©™ 3 Website Framework | File download and article manager component by K2 | Advanced Module Manager and Modules Anywhere by NoNumber Elements | Website hosting and development by WebsiteDons

CMSEnergizer.com is owned and operated by Emuzement Net Inc, 4532 West Kennedy Blvd. Ste 180 33609 Tampa, Florida USA