Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Session Data in Hook in CodeIgniter

I am using codeigniter I want to use Hooks for login authentication. That means i want in every controller check the session data and time logged in. for tht i inserted loggin time in session .if loggedin or not and time since he logged in. so that i want to use hooks. I want to access session->userdata values from array but i cant figure how to get those value from tht array .I want to get logged time from array and update whenevr user clicks or navigate on site.This is My code:

//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
            }
        }
    }
}

I have Data in:

$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 ) )

How can i retrive ID,user_name in hook ??

like image 454
Bhushan Avatar asked Aug 20 '14 16:08

Bhushan


2 Answers

function loginCheck()
{
    $session_userdata = $this->CI->session->userdata('logged_in');
    if($session_userdata['uid']=="XYZ")
    {
        echo "Valid User"; //it wont get inside this if
    }
}
like image 198
skmail Avatar answered Oct 22 '22 10:10

skmail


config/hooks.php

    $hook['post_controller_constructor'][] = array(
        "class"    => "Validate_Session",
        "function" => "validate",
        "filename" => "Validate_Session.php",
        "filepath" => "hooks"
    );
  • Create a custom library to access CI session library.

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;
        }
    }
}
  • Inside hooks class constructor load custom created library.
  • __get magic methods makes sure that all properties are available to access the CI Session Library.
  • validate() method access the session variable through custom library's method isSessionExists() and this method returns false if session variable does not exists and returns the session variables value if exists.

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;
        }
    }
}
like image 40
Pravin Shinde Avatar answered Oct 22 '22 10:10

Pravin Shinde