Search code examples
sessioncodeigniterauthentication

Codeigniter Users Login Check


I'm a novice with Code Igniter and I'm currently building my own user system. I'm curretly on the login process and I have implemented a check for whether a user is currently logged in or not.

In my header I then want to display a link to 'Log Out' if they are already logged in, or 'Log In' if they are not logged in currently.

I have a working function in my index controller as follows, the $loginstatus variable is sent to the my page header view:

function check_session()
{
    //Check session status

            $session = $this->session->userdata('login_state'); 

            $default = "Log In";

            if ($session == 1) 
            {
                $url = site_url('index.php/users/logout'); 
                $status = "Log Out";



            } 
            else 
            {
                $url = site_url('index.php/users/login'); 
                $status = $default;
            }

        $loginstatus = array(
                        "url" => $url,
                        "status" => $status 
                        );

        return $loginstatus;
}

Because it is currently only in the index controller the $loginstatus is not generated for the header view for other pages and this is my problem.

Where would I put this function so that it always loads before my header? I tried creating a libary with a 'Common' class and then autoloading that but I ended up with lots of problems.

Thanks in advance.


Solution

  • If you are using CI version bellow 2.0 then create new class in application/libraries/MY_Controller.php, otherwise in application/core/MY_Controller.php and all of your application controllers should extend from it. In this class in the __construct method you will check for the login status and send it to the views.

    class MY_Controller extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
    
            //Get the status which is array
            $login_status = $this->check_session();
    
            //Send it to the views, it will be available everywhere
    
            //The "load" refers to the CI_Loader library and vars is the method from that library.
            //This means that $login_status which you previously set will be available in your views as $loginstatus since the array key below is called loginstatus.
            $this->load->vars(array('loginstatus' => $login_status));
        }
    
        protected function check_session()
        {
            //Here goes your function
        }
    }
    

    Also make sure your application controllers extend from this class

    //application/controllers/index.php
    
    class Index extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    }
    

    In your views you can do this: <a href="<?php echo $loginstatus['url']; ?>"><?php echo $loginstatus['status']; ?></a>

    This is possible cause the CI_Loader vars() method is doing extract to the parameters passed to it.