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 ??
function loginCheck()
{
$session_userdata = $this->CI->session->userdata('logged_in');
if($session_userdata['uid']=="XYZ")
{
echo "Valid User"; //it wont get inside this if
}
}
config/hooks.php
$hook['post_controller_constructor'][] = array(
"class" => "Validate_Session",
"function" => "validate",
"filename" => "Validate_Session.php",
"filepath" => "hooks"
);
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;
}
}
}
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With