Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 7 revisions

Categories:Helpers | Categories:Helpers::Community

[h4]IP Bouncer[/h4]

This helper function, written by [url=http://www.twitter.com/seangates]Sean Gates[/url], 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.

[b]NOTE:[/b] 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.

[code] <?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('173.162.26.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();
    }
}

} [/code]

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

Clone this wiki locally