【问题标题】:Use session data in Hook in CodeIgniter 2.1.0在 CodeIgniter 2.1.0 的 Hook 中使用会话数据
【发布时间】:2012-03-24 08:33:32
【问题描述】:

我正在使用版本为 2.1.0 的 CodeIgniter。我想使用 Hooks 进行登录验证。这意味着我希望在每个控制器中检查会话数据是否已登录。所以我想使用钩子。为此,我执行以下代码:

在配置文件中

$config['enable_hooks'] = TRUE;

在文件hooks.php

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

在文件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');
        }
    }
}

但它会引发以下错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: SessionData::$session

Filename: hooks/loginHelper.php

Line Number: 13

我该如何解决这个问题?

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    "在系统执行过程中很早就被调用。此时只加载了基准和钩子类..."

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

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

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

    【讨论】:

    • 第一个短语是pre_system now的描述。以前不确定。
    【解决方案2】:

    正如 safarov 所提到的,当你的钩子运行时,CodeIgniter 系统不会加载任何库,只加载基准和钩子库。此时,您可以使用在 Controller 运行时加载的任何 CodeIgniter 功能。

    因此,在您的课程sessionData 中,您必须使用 CodeIgniter 超级对象加载会话类。

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

    上面的代码是你修改后的代码,我把给你提到的代码safarov放在了。

    【讨论】:

      【解决方案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】:

        我假设您忘记加载会话库。

        【讨论】:

          【解决方案5】:

          解决方案是扩展 CI_Controller 示例:

          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);
                }
            }
            
            1. 您必须使用 get_instance 才能访问会话库

            2. 将成员会话 ID 保存在 $member_id 中

            3. 加载成员的库

            4. 在成员库方法中使用$member_id

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-07-14
              • 1970-01-01
              • 1970-01-01
              • 2013-10-11
              • 1970-01-01
              • 1970-01-01
              • 2016-06-19
              • 1970-01-01
              相关资源
              最近更新 更多