ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to use before_filter for auth

November 22, 2007 5:28am

Subscribe [3]
  • #1 / Nov 22, 2007 5:28am

    gayathri

    6 posts

    Hi All,

      Basically i am a Ruby on rails developer, but now i am working with CI, now i want to before_filter to authorize the login to all controller, i got some idea through the Filter system (http://codeigniter.com/wiki/Filters_system/), i downloaded this file and i replaced it where its require, but i don’t know how to start and how to define in the controller.
    because there is no clear example for this. for example in ruby on rails i have done the code like this in

    application controller

    def authorize
      unless session[:user]
      flash[:notice]=“Please log in”
      redirect_to(:controller => “login” , :action => “index”) 
      end
    end

    in

    login controller

    i write the code like this,

    before_filter :authorize, :except =>[:index]

          so, can i anyone tell, how to use the before_filter in php with example. thanks in advance.

  • #2 / Nov 22, 2007 8:39am

    Chris Newton

    440 posts

    Because the filters are set to run BEFORE the controller, you shouldn’t add anything to your controller at all. You should be adding any relevant code to your filter system.

    1. Configure your config/hooks.php file

    2. Configure the config/filter.php file with the name of the filter you plan to use; (something like this)

    $filter['auth'] = array('exclude', array('/', 'login/*'), array());

    3. Create your filter and add it to the filters folder, if your filter is named ‘auth’ you’d add it like this; filters/auth.php

    <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
    /**
    * Test filter - logs message on filter enter and exit
    */
    class Auth_filter extends Filter {
        function before() {
          // add login stuff
        }
        
        function after() {
            // whatever
        }
    }
    ?>

    I prefer to just add some code to my controller and use one of the login libraries rather than using a filter, something like this; http://codeigniter.com/wiki/Simplelogin/

    If you set something like that up, you can simply add this to the beginning of all of your controllers;

    if(!$this->session->userdata('logged_in')) {
            redirect('login', 'location');
            }
  • #3 / Nov 22, 2007 8:48am

    gayathri

    6 posts

    hi,

      Thanks for your reply, i feel your suggestion is nice,

    if(!$this->session->userdata(‘logged_in’)) {
    redirect('login', 'location');
    }

    this code, should i add for each and every function or only top of the controller, and if i want any expectional page, how to use that one? can you tell me please?

  • #4 / Nov 22, 2007 11:24am

    Chris Newton

    440 posts

    You need to add that to every controller function that is going to render a view (public controller functions)

    Also, you still have to set up a login system that uses sessions. Any PHP page can read the information out of the session, so you could put something like that on non-CI pages.

    On a normal PHP page, it might be something like this;

    if (!$_SESSION[‘logged_in’]) {
    header('location: index.php/login');
    }

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases