codeigniter check for user session in every controller

前端 未结 7 1532
再見小時候
再見小時候 2020-11-30 23:54

I have this private session in one of my controllers that checks if a user is logged in:

function _is_logged_in() {

   $user = $this->session->userdat         


        
相关标签:
7条回答
  • 2020-12-01 00:37

    Put it in a helper and autoload it.

    helpers/login_helper.php:

    function is_logged_in() {
        // Get current CodeIgniter instance
        $CI =& get_instance();
        // We need to use $CI->session instead of $this->session
        $user = $CI->session->userdata('user_data');
        if (!isset($user)) { return false; } else { return true; }
    }
    

    config/autoload.php:

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

    Then in your controller you can call:

    is_logged_in();
    
    0 讨论(0)
  • 2020-12-01 00:41

    Another option is to create a base controller. Place the function in the base controller and then inherit from this.

    To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.

    class MY_Controller extends Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function is_logged_in()
        {
            $user = $this->session->userdata('user_data');
            return isset($user);
        }
    }
    

    Then make your controller inherit from this base controller.

    class X extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function do_something()
        {
            if ($this->is_logged_in())
            {
                // User is logged in.  Do something.
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 00:42

    You can achieve this using helper and CodeIgniter constructor.

    1. You can create custom helper my_helper.php in that write your function

      function is_logged_in() {
        $user = $this->session->userdata('user_data');
        if (!isset($user)) { 
         return false; 
        } 
       else { 
         return true;
       }
      } 
      
    2. In controller if its login.php

      class Login extends CI_Controller {
      
          public function __construct()
          {
              parent::__construct();
              if(!is_logged_in())  // if you add in constructor no need write each function in above controller. 
              {
               //redirect you login view
              }
          }
      
    0 讨论(0)
  • 2020-12-01 00:47

    I coded like this according to above answers.. And this is running for me Create file my_helper.php

    <?php 
        function _is_logged_in() {
            if(isset($_SESSION['username'])){
                return true;        
            } else {
                return false;
            }
        }
    ?>
    

    Edit in autoload.php file

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

    In your Controller file

    class Welcome extends CI_Controller {
    
        public function __construct(){
            parent::__construct();
    
            if(!_is_logged_in())  
            {
                redirect("Login");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 00:49

    Get all user's data from session.

    In the Controller,

    $userData = $this->session->all_userdata();
    

    In the View,

    print_r($userData);
    
    0 讨论(0)
  • 2020-12-01 00:53

    I think using hooks is pretty easy. Just create a hook to check $this->session->user. It will be called in every request.

    0 讨论(0)
提交回复
热议问题