在codeigniter 2.1.0中使用Hook中的会话数据 [英] use session data in Hook in codeigniter 2.1.0

查看:128
本文介绍了在codeigniter 2.1.0中使用Hook中的会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用codeigniter版本2.1.0。我想使用Hooks进行登录验证。这意味着我想在每个控制器检查会话数据是否logedin。所以我想使用钩子。我做以下代码:



$ config ['enable_hooks'] = TRUE;

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

  class SessionData {
var $ CI;

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

function initializeData(){
//此函数将在控制器的构造函数后运行
//在此设置任何初始值
if(!$ this-> session-> userdata('username')){//这是第13行
redirect('login');
}
}
}

/ p>

但它会引发以下错误:

 遇到PHP错误

严重性:注意

消息:未定义属性:SessionData :: $ session

文件名:hooks / loginHelper.php

行号:13

我如何解决这个问题?提前感谢。

解决方案

在系统执行的早期调用只有基准和钩子类被加载this point ...



因此,您应该手动加载在Hook中使用的所有库和模型

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

并使用 $ this-> CI-> ; session-> userdata()而不是 $ this-> session-> userdata()


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 logedin or not. so that i want to use hooks. i do the following code for do that:

$config['enable_hooks'] = TRUE; in config file.

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

in hooks.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');
          }
    }
}

in loginHelper.php.

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? Thanks in advance.

解决方案

"Called very early during system execution. Only the benchmark and hooks class have been loaded at this point..."

SO you should load all library and model 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()

这篇关于在codeigniter 2.1.0中使用Hook中的会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆