Articles → PHP CODEIGNITER → Check If User Is Logged In Or Not In Codeigniter
Check If User Is Logged In Or Not In Codeigniter
Scenario
- user_list
- Welcome
Steps
- Create a new class "MY_Controller" which will extend "CI_Controller" class.
- Save the file in the "Core" folder.
- Inherit the "user_list" from "MY_Controller" class.
- 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
Click to Enlarge