Welcome Guest, Not a member yet? Register   Sign In
How to use before_filter for auth
#1

[eluser]gayathri[/eluser]
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
Quote:application controller

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

in
Quote: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

[eluser]Chris Newton[/eluser]
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)

Code:
$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
Code:
<?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;
Code:
if(!$this->session->userdata('logged_in')) {
        redirect('login', 'location');
        }
#3

[eluser]gayathri[/eluser]
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

[eluser]Chris Newton[/eluser]
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');
}




Theme © iAndrew 2016 - Forum software by © MyBB