Skip to content
Derek Jones edited this page Jul 5, 2012 · 7 revisions

Categories:Helpers | Categories:Helpers::Community

IP Bouncer

This helper function, written by Sean Gates, allows you to easily restrict a controller, or method, by a visitor's IP address.

This could easily be done in each controller, but I decided to write a small helper.

NOTE: The "if" statement also includes a check for the script being located in a subdomain path. This is for a "dev" and "staging" setup where the the dev and staging servers are subdomains of the main URL. This is specific to MediaTemple's (dv) v3.0 servers using Plesk.

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/** 
 * Checks an ip address (and whether the server is dev or staging on a MediaTemple (dv) subdomain),
 * prints a message, and ends page execution if the IP isn't allowed.
 *
 * Use it in the instantiation block of any controller to restrict the page, or in any method
 * you want restricted.
 * 
 * @author         Sean Gates
 * @link         http://seangates.com
 * @license        GNU Public License (GPL)
 * 
 * @access         public
 * @param         string [$ip] valid IP address
 * 
 * USAGE:
 *
 *         class Home extends Controller {
 *         
 *             function Home()
 *             {
 *                 parent::Controller();
 * 
 *                 // bounce the person if they don't have the right IP address
 *                 ip_bouncer($this->input->ip_address());
 *             }
 * 
 *             ...
 *         }
 * 
 */
if (! function_exists('ip_bouncer'))
{
    function ip_bouncer($ip)
    {        
        // restrict to these IP addresses
        $ip_addresses = array('12.124.10.18');
        
        // check if the ip is allowed, and whether we're on the dev or staging servers
        if( !in_array($ip, $ip_addresses) && strstr(getcwd(),'/subdomains/') )
        {
            echo 'This is a restricted area. Move along ...';
            exit();
        }
    }
}

Category:Contributions Category:Contributions::Helpers Category:Contributions::Helpers::Miscallenous

Clone this wiki locally