Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not giving access to certain method in controller when session is not set in codeigniter

I want user to not access certain method of controller when session is not set. For this I can check session in all method and if session is set then only go to furthur else redirect to specific page. Since I have many method that I don't want user to take access if session is not set. Its bulk to go through all method and check session. Are there any shortcut way to obtain this functionality.

I tried checking session is constructor method of controller but it works for all method. But I want only specific method to block if session is not set. How to do it.

Example:

class dashboard extends CI_Controller {

 function __construct() {
    parent::__construct();
    $this->load->library('session');
    $this->load->model('dbmodel');
    $this->load->helper('url','form');


    //verified user check
    if($this->session->userdata("unverified") != FALSE) {
        redirect("verify_user");  

    }

    }
    //verified user check
}

Above code, redirects to verify_user controller as soon as 'unverified' session is found when user go to dashboard controller. But I want to give access to some method of dashboard controller. Not all method. Where as this code redirects whenever session is found and don't give access to any method of dashboard controller.

like image 473
user254153 Avatar asked Jul 07 '15 06:07

user254153


People also ask

How to call controller method in CodeIgniter?

By default Controller always calls index method. If you want a different method, then write it in the Controller's file and specify its name while calling the function. Look at the URL, there is no method name is mentioned.

What is ci_ controller in CodeIgniter?

The pages class is extending the CI_Controller class. This means that the new pages class can access the methods and variables defined in the CI_Controller class ( system/core/Controller. php ). The controller is what will become the center of every request to your web application.

How to set default controller in CodeIgniter?

Defining a Default Controller CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog ';

How to access controller in CodeIgniter?

CodeIgniter permits you to do this. Simply create sub-directories under the main application/controllers/ one and place your controller classes within them. Each of your sub-directories may contain a default controller which will be called if the URL contains only the sub-directory.


1 Answers

Check this It might help you

class MY_controller extends CI_controller{
    function __construct() {
        parent::__construct();
    }
    function _required_user($params =array()){
        $action =$this->router->fetch_method();
        if(empty($params['except']))
            $params['except'] =array();
        if(empty($params['only']))
            $params['only'] =array();
        if(count($params['except']) > 0 && in_array($action,$params['except']))
            return true;    
        if(count($params['only']) > 0 && in_array($action,$params['only']) && $this->session->userdata('is_login'))
            return true;
        if($this->session->userdata('is_login'))    
            return true;
        redirect('login');  

    }       
}

class dashboard extends MY_Controller {

  function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('dbmodel');
        $this->load->helper('url','form');
        $this->_required_user(array('except'=>array('index')))      
    }
    function add(){
    /*
    Session required
    */  
    }
    function edit(){
    /*
    Session required
    */  
    }
    function index(){
    /*
    no session required
    */
    }
  }

 class content extends MY_Controller{
    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('dbmodel');
        $this->load->helper('url','form');
        $this->_required_user(array('only'=>array('index')))        
    }
    function add(){
    /*
    no Session required
    */  
    }
    function edit(){
    /*
    no Session required
    */  
    }
    function index(){
    /*
    session required
    */
    }
 }  
class Myaccount extends MY_Controller{
    function __construct() {
        parent::__construct();
        /*
        for all functions session required
        */
        $this->_required_user()     

    }
    function edit(){
    /*
    session required
    */
    }
    function save(){
    /*
    session required
    */
    }
 }
  1. Only param: check session is exist for only given functions/function
  2. Except param: Don't check session for given functions/function
  3. No Param : check session for all function in controller and redirect

You can modified _reuired_user function according to your requirement

like image 97
shafiq.rst Avatar answered Sep 26 '22 08:09

shafiq.rst