当未在编码器中设置会话时,不允许访问控制器中的某些方法


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

我希望用户在会话未设置时不访问控制器的某些方法。为此,我可以在所有方法中检查会话,如果会话被设置,那么只去进一步重定向到特定的页面。因为我有很多方法,我不希望用户访问,如果会话没有设置。通过所有的方法和检查会话是很大的。有什么捷径可以获得这个功能吗?

我尝试检查会话是控制器的构造函数方法,但它适用于所有方法。但我只想要特定的方法来阻止,如果会话没有设置。如何去做。

的例子:

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
}

上面的代码,重定向到verify_user控制器一旦'unverified'会话被发现,当用户去仪表板控制器。但我想给一些仪表板控制器的方法。不是所有的方法。当找到会话时,此代码将重定向,并且不允许访问仪表板控制器的任何方法。

检查这个,它可能对你有帮助

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. 只有参数:检查会话只存在于给定的函数/函数
  2. Except参数:不检查给定函数的会话/function
  3. 无参数:检查会话的所有功能在控制器和重定向

您可以根据需要修改_reuired_user函数

您可以尝试以下操作-如果它有帮助的话

在application/core文件夹中创建一个名为MY_Controller的类就像

class MY_Controller extends CI_Controller
{
    public function checkUserSession()
    {
        if($this->session->userdata("unverified") != FALSE) 
        {
            redirect("verify_user");
        }
        return true;
    }
}

之后,你的仪表板控制器应该看起来像

class dashboard extends MY_Controller 
{
    public function show_dashboard()
    {
        if ($this->checkUserSession())
        {
            //your code
        }
    }
}

创建一个名为Users的库

class Users 
{
    public function __construct()
    {
        $this->ci =& get_instance();
    }
    function _is_logged()
    {
        if(!$this->ci->session->has_userdata('unverified'))
        {
            redirect('verify_user');
        }
    }
}

加载库或将其放入application/config/autoload.php中的自动加载

然后调用你想要限制的方法顶部的函数。

function abc()
{
    $this->users->_is_logged();
    // your code
}

这不是一个答案,而是根据我的经验提出的建议:

作为一个开发人员,如果像上面提到的@user1048123的答案那样做是很容易的。

但是,如果您正在访问不需要会话的方法(ex: index),但仍然必须在索引显示之前处理function _required_user()方法,您必须传递更大的数组(ex: 30方法在数组名称中)。这将减慢不需要会话的方法的加载时间,因此为了更好的性能检查,会话是需要检查会话的方法(在每个方法中)。

这个想法可能是旧的,但当你有更多的方法或用户更大时,它真的会影响性能。

你能像这样检查吗?

function __construct()
{
    parent::__construct();
}
function chk_login()
{
    if($this->session->userdata('logged_in'))
    {
         echo "some action";
    }
    else
    {
         redirect('login');
    }
 }

In Controller

<?php
    class dashboard extends CI_Controller {
        function __construct()
        {
            parent::__construct();
            $this->load->library('session');
            $this->load->model('dbmodel');
            $this->load->helper('url','form');

        }
        public function index()
        {
            $user = $this->session->userdata("unverified")//asign session value to variauble
            $result = $this->dbmodel->check_user($user);//check user validity
        if(empty($result))
        {
            //user is not verified
            $this->load->view('unverified_loging');
        }
        else
        {
            //user is verified
            $this->load->view('verified_loging');
        }
    }
模型中

    public function check_user($user)
    {
        $query = $this->db->query("SELECT * FROM user WHERE <argument here>");//check with database
        $result = $query->result_array();
        return $result;
    }

如果用户对validity用户满意,则将数据传递给$result。否则,如果不满意validity,它将返回$result作为NULL

在控制器中你可以检查$result是否为空