问题
I am using CodeIgniter with version 2.1.0. I want to use Hooks for login authentication. That means I want in every controller check the session data if loggedin or not. So I want to use hooks. I do the following code for doing that:
In 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 it 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?
回答1:
"Called very early during system execution. Only the benchmark and hooks class have been loaded at this point..."
You should load all libraries and models manually that you use inside Hook:
if (!isset($this->CI->session))
{
$this->CI->load->library('session');
}
And use $this->CI->session->userdata() instead of $this->session->userdata().
回答2:
As mentioned by safarov, at that point when your hook is running, no libraries are loaded by the CodeIgniter system and only benchmark and hooks libraries are loaded. At that point you can use any CodeIgniter features which are loaded at Controller running time.
So in your class sessionData you have 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 placed the code safarov mentioned to you.
回答3:
<?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;
}
}
回答4:
I am assuming that you forgot to load your session library.
回答5:
The solution is extending the CI_Controller example:
class SessionData extens CI_Controller {
$this->load->library('session');
# Code ..
}
回答6:
<?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 have to use the get_instance so you can access the library of the session
Save the member session id in $member_id
Load the library of the member
Use the $member_id in the method of member library
来源:https://stackoverflow.com/questions/9850247/use-session-data-in-hook-in-codeigniter-2-1-0