42

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->userdata('user_data');

   if (!isset($user)) { 
      return false; 
   } 
   else { 
      return true;
   }

}

Problem is that I have more than one Controller. How can I use this function in those other controllers? Redefining the function in every Controller isn't very 'DRY'.

Any ideas?

el_pup_le
  • 11,711
  • 26
  • 85
  • 142

7 Answers7

56

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.
        }
    }
}
Stephen Curran
  • 7,433
  • 2
  • 31
  • 22
  • Didn't think of that, I really like the idea. I think it's a bit more practical than using a helper. –  Sep 10 '10 at 02:00
  • 1
    My controller doesn't get detected, i get this error: Fatal error: Class 'MY_Controller' not found in C:\xampplite\htdocs\mobilehome\application\controllers\links.php on line 3 ............ any idea how I can autoload it somewhere? Or should I just include it on top of every controller? –  Sep 10 '10 at 14:58
  • 3
    I think the controllers need constructors. `function __construct(){parent::__construct();}` – gen_Eric Sep 10 '10 at 15:04
  • Sorry Sied. Rocket is right. I had forgotten the constructors. I've updated the answer. – Stephen Curran Sep 11 '10 at 08:32
  • Thank you for a great down to earth answer which explained things well. – Adamantus Mar 16 '12 at 14:25
40

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();
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • how do I call the session class from outside a class? I'm getting this error with your code: Fatal error: Using $this when not in object context in C:\xampplite\htdocs\mobilehome\application\helpers\login_helper.php on line 4 ... thanks! –  Sep 10 '10 at 15:10
  • 3
    You don't use `$this` in a helper. Please look again at the code in the answer. In helpers (and libraries), to use CodeIgniter functions, you need to get the CodeIgniter instance. `$CI =& get_instance();` does this. Instead of using `$this`, use `$CI`. – gen_Eric Sep 10 '10 at 16:30
  • @RocketHazmat, this is great idea bro – jvk Aug 17 '18 at 17:14
6

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
            }
        }
    
Pang
  • 9,564
  • 146
  • 81
  • 122
prash.patil
  • 687
  • 8
  • 7
3

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

Rakesh
  • 31
  • 4
2

Get all user's data from session.

In the Controller,

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

In the View,

print_r($userData);
Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35
2

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");
        }
    }
}
Huseyin
  • 567
  • 6
  • 17
1

Just add this on your folder core file ci_controller at function __construct() to check all controller ():

function __construct() 
{
    parent::__construct();
    if(! $user = $this->session->userdata('user_data');)
    {
         return false;
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122