Best way to implement an IP Filter

Hey gurus!

I was wondering what you guys think is the best way to implement an IP filter, and also how I should go about executing a function on every page.

I have a model and controller that allows my end users admins to enter IP’s that they want to block from having access to the site, they want to redirect these blocked IP’s to an error page. I’ve written the controller function to check the IP of the visitor and redirect but am unsure how to have the function execute on every page without having to code it in all controllers.

Thanks

Sn0rcha

  • You could use a parent-controller and make use of the beforeAction() method.

  • You could make use of the onBeginRequest event.

  • You could preload a special component.

Last solution could look like this:




class IPBlocker extends CApplicationComponent

{


   public function init()

   {


      // check if ip is blocked...


   }


}



Then in config:




...


preload => array('log', 'ipblocker'),


...


'components' => array(

   ...

   'ipblocker' => array(

      'class' => 'application.components.IPBlocker',

   ),

   ...

),



You could also use a filter. There is already CAccessControlFilter which has functionality for blocking IP-addresses.

If you want custom functionality you could create a class that extends from this or CFilter and then define it in the filters()-method of your parent controller.

Thanks for the replies guys, i’m still quite new at Yii but am loving it :)