2

I am writing a web application using codeigniter.
I want to authenticate the privilege of our users before they access the page.
Actually nearly all controller action except the log in page need to call the model and use

$this->Users->validate($username, $password)

I want to make it general for every controller. Do I need to inherit the controller class? How could i do that?

5 Answers 5

13

We have a project using Codeigniter and the way we are doing it :

When you have a new controller :

class xxxx extends MY_Controller { }

And Inside the MY_Controller class

function __construct() {
 // do your validations
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, using a parent controller for all controllers is the best way to handle this, and will be handy for other stuff too. If you have a few pages like a login page that are open you can create an array $open_pages('users/login','users/register') and check those against the URL before you check for credentials
2

The best way is that you should make a helper file in your application/helper folder with this name or any of you want but don't remove _helper, you should use this name authentication_helper, and put the following code as yours

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

    function varify_session(){
       $CI = &get_instance();
       $user_session_id = $CI->session->userdata('logged_in');

       if($user_session_id  ==  '') {
        redirect('login');
       }
   }
   ?>  

chage code with yours code. then in your autoload file and in put this into helper

     $autoload['helper'] = array('authentication');

then you just need to put this line into your every controller constructor like this

     function __construct()
     {
         parent::__construct();
         varify_session();  
     }

hope it will help.

Comments

1

please write below code in constructor of each controller

$this->load->library(‘session’);
$this->load->model(‘login_model’,'login’,TRUE);

/* check whether login or not */
if(!$this->login->check_session()){
redirect(‘/login’);
}

Comments

0

Correct before filter usage:

class Test extends Controller {

    var $before_filter = array();

    var $after_filter = array();

    function __construct() {
        parent::Controller();

        $this->before_filter[] = array(
            'action' => 'before_filter_run',
        );

    }

    function before_filter_run() {
        // Auth here
    }
}    

For details read here

Comments

0

in CodeIgniter 4, you should put your logic in BaseController.php

public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger){
            // Do Not Edit This Line
            parent::initController($request, $response, $logger);
    
            // Preload any models, libraries, etc, here.
            // E.g.: $this->session = \Config\Services::session();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.