Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

php - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...