Block Selected IPs From Specific Pages In Joomla

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

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

<?php defined('_JEXEC') or die();
* @package      plg_system_the_ip_control
* @copyright   Copyright (C) All rights reserved.
* @license      GNU General Public License version 2 or later; see

class plgSystemThe_IP_Control extends JPlugin {

   public function onAfterInitialise() {
      //do not render the function in admin area
         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') );


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

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.7.0" type="plugin" group="system" method="upgrade">
   <name>System - The IP Control</name>
   <copyright>All Rights Reserved.</copyright>
   <license>license GNU/GPLv3</license>
   <description>Allow or Deny IP</description>
      <filename plugin="the_ip_control">the_ip_control.php</filename>
   <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 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="" 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" />


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