Use Session Data in Hook in CodeIgniter 2.1.0

I am using CodeIgniter with version 2.1.0. I want to use Hooks for login authentication. This means that I want the session data to be checked in each controller if it has been registered or not. So I want to use hooks. For this I am doing the following code:

In the config file

$config['enable_hooks'] = TRUE;

      

In file hooks.php

$hook['post_controller_constructor'][] = array(
                               'class'    => 'SessionData',
                               'function' => 'initializeData',
                               'filename' => 'loginHelper.php',
                               'filepath' => 'hooks',
                               'params'   => array()
                               );

      

In file loginHelper.php

class SessionData{
    var $CI;

    function __construct(){
        $this->CI =& get_instance();
    }

    function initializeData() {

        // This function will run after the constructor for the controller is ran
        // Set any initial values here
        if (!$this->session->userdata('username')) { // This is line 13
            redirect('login');
        }
    }
}

      

But this throws the following error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: SessionData::$session

Filename: hooks/loginHelper.php

Line Number: 13

      

How can I solve this?

+3


source to share


6 answers


"Called very early during system execution. At this point only the test and hook class is loaded ..."

You have to load all libraries and models manually that you use inside the Hook:



if (!isset($this->CI->session))
{
    $this->CI->load->library('session');
}

      

And use $this->CI->session->userdata()

instead $this->session->userdata()

.

+7


source


As mentioned by safarov, the moment your hook is running, the libraries are not loaded by the CodeIgniter system and only the test and interceptor libraries are loaded. At this point, you can use any CodeIgniter functions that are loaded while the controller is running.

So, in your class, sessionData

you need to load the session class using the CodeIgniter super object.



class SessionData {
    var $CI;

    function __construct(){

        $this->CI =& get_instance();
        if(!isset($this->CI->session)) // Check if the session library is loaded or not
            $this->CI->load->library('session'); // If not loaded, then load it here
    }

    function initializeData() {

        // This function will run after the constructor for the controller is ran
        // Set any initial values here
        if (!$this->CI->session->userdata('username')) { // Call session methods with super object
            redirect('login');
        }
    }
}

      

The above code is your modified code and I put the safar code you mentioned.

+1


source


I am assuming that you forgot to load your session library.

0


source


The solution expands on the CI_Controller example:

class SessionData extens CI_Controller {
  $this->load->library('session');
  # Code ..
}

      

0


source


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class sessiondata extends CI_Controller{
    function initializeData(){
        **// imports libraries.**
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('form');
        if(!$this->session->userdata('email')):
            redirect('login');
        endif;
    }

}

      

0


source


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

class MembersLoader
{
    function initialize() {
        $CI =& get_instance();

        // Load Session
        $CI->load->library('session');
        $member_id = $CI->session->userdata('userid');

        // Load Members Stuff
        $CI->load->library("members");
        $CI->members->set_members_data($member_id);
    }
}

      

  • You need to use get_instance so that you can access the session library

  • Store the member's session id to $ member_id

  • Load the element library

  • Use $ member_id in member library method

0


source







All Articles