1

I am new to CodeIgniter. I want to create a new controller which checks all other controller and methods if the user logged in already. I am using session by the way. As stated here CodeIgniter - How to check session to be used at every methods and here .

I need to create My_Controller.php inside application/core folder.

<?php
class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
}
?>

Public_Controller.php

<?php
class Public_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        // do some session stuff here :)
    }
}

Admin_Controller.php

<?php
class Admin_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        // do some session stuff here :)
    }
}

What should I add inside the 2 new controller so I can use it in controller like base.php? Or how can I implement the session checking? Sorry, it is very hard for me to explain.

Community
  • 1
  • 1
Port 8080
  • 858
  • 3
  • 12
  • 24
  • possible duplicate of [CodeIgniter - How to check session to be used at every methods](http://stackoverflow.com/questions/8259617/codeigniter-how-to-check-session-to-be-used-at-every-methods) – Saswat Aug 18 '14 at 08:36

1 Answers1

3

Make ur My_controller be like this:

<?php
class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('session'); // if you are loading session library automatically in the autoload model, then you dont need this line.
        /*---- you can do the session related work in the My_Controller as well if needed-----*/
        if($this->session->userdata('logged_user')== true)
           {
               // session related work;
           }
    }
}
?>

then in the public controller make the following changes

<?php
include('my_controller.php');
class Public_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        /*---- if session checking is done in the My_Controller then you might not need to do it here again---*/
        if($this->session->userdata('logged_user')== true)
           {
               // session related work;
           }
    }
}

similarly proceed with the other controller

Saswat
  • 12,320
  • 16
  • 77
  • 156