在CodeIgniter中获取Hook中的会话数据

时间:2014-08-20 16:53:38

标签: php arrays codeigniter session

我正在使用codeigniter我想使用Hook进行登录验证。这意味着我希望在每个控制器中检查会话数据和登录的时间。因为我在会话中插入了登录时间。如果登录或没有登录,那么自从他登录以来的时间。所以我想使用挂钩。 我想从数组访问session-> userdata值,但我不知道如何获取它们 来自数组的值。我想从数组获取记录时间并更新whenevr用户点击或在网站上导航。这是我的代码:

//enter code here
$hook['post_controller_constructor'][] = array(
                           'class'    => 'Authenticate',
                           'function' => 'loginCheck',
                           'filename' => 'authenticate.php',
                           'filepath' => 'hooks',
                           'params'   => array()
                           );

class Authenticate
{

    private $CI;

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

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

   function loginCheck()
   {

        if(!$this->CI->session->userdata){
            if($this->CI->session->userdata('uid')=="XYZ")
            {
                echo "Valid User"; //it wont get inside this if
            }
        }
    }
}

我有数据:

$this->CI->session->userdata array :
Array ( [session_id] =>afaf... [ip_address] => ::1 [user_agent] => Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36 [last_activity] => 1408553094 [user_data] => [logged_in] => Array ( [Id] => 1 [user_name] => buzz [login_time] => 22:14:54 [uid] => XYZ ) )

如何在钩子中检索ID,user_name

2 个答案:

答案 0 :(得分:4)

function loginCheck()
{
    $session_userdata = $this->CI->session->userdata('logged_in');
    if($session_userdata['uid']=="XYZ")
    {
        echo "Valid User"; //it wont get inside this if
    }
}

答案 1 :(得分:1)

config / hooks.php

    $hook['post_controller_constructor'][] = array(
        "class"    => "Validate_Session",
        "function" => "validate",
        "filename" => "Validate_Session.php",
        "filepath" => "hooks"
    );
  • 创建自定义库以访问CI会话库。

libraries / Authenticate.php

<?php

class Authenticate{

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

    public function isSessionExists(){

        if($this->CI->session->has_userdata('key')){
            return $this->CI->session->userdata('key');
        }else{
            return false;
        }
    }
}
  • 内部挂钩类构造函数加载自定义创建的库。
  • __ get magic方法可确保所有属性都可用于访问CI会话库。
  • validate()方法通过自定义库的isSessionExists()方法访问会话变量,如果会话变量不存在,则此方法返回false;如果存在会话变量,则返回该会话变量的值。

hooks / Validate_Session.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Validate_Session
{
    public function __construct()
    {
        $this->load->library('authenticate');
    }

    public function __get($property)
    {
        if ( ! property_exists(get_instance(), $property))
        {
                show_error('property: <strong>' .$property . '</strong> not exist.');
        }
        return get_instance()->$property;
    }

    public function validate()
    {
        if ( ! $this->authenticate->isSessionExists())
        {
                //redirect in login
            echo "Invalid Session";
            exit;
        }else{
            $loginUserData = $this->authenticate->isSessionExists();
            echo $loginUserData;
            exit;
        }
    }
}