Articles → PHP CODEIGNITER → Check If User Is Logged In Or Not In Codeigniter

Check If User Is Logged In Or Not In Codeigniter






Scenario




  1. user_list
  2. Welcome



Steps




  1. Create a new class "MY_Controller" which will extend "CI_Controller" class.
  2. Save the file in the "Core" folder.
  3. Inherit the "user_list" from "MY_Controller" class.
  4. Check if the user is logged-in or not in the constructor of the "MY_Controller" class.


<?php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this
            ->load
            ->library("session");
        $this
            ->load
            ->helper("url");

        if (!$this
            ->session
            ->userdata("userid"))
        {
            echo redirect(base_url() + 'Welcome');
        }
    }
}




<?php
defined('BASEPATH') or exit('No direct script access allowed');

class users_list extends MY_Controller
{
    public function Index()
    {
    }
}



Output




Picture showing the page redirection when session is not set
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, August 14, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250