get Session Data in Hook in CodeIgniter

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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 ??


ID,user_name




2 Answers
2


function loginCheck()

$session_userdata = $this->CI->session->userdata('logged_in');
if($session_userdata['uid']=="XYZ")

echo "Valid User"; //it wont get inside this if






Thank you soo much ! :)
– Bhushan
Aug 20 '14 at 17:26





Bt i hv another question how can i update userdata in CI session array from Hook??
– Bhushan
Aug 20 '14 at 17:27





$logged_in = array("uid"=>"xsss2"); $session_userdata = $this->CI->session->set_userdata('logged_in',$logged_in);
– Solaiman Kmail
Aug 20 '14 at 17:28






thanks for ur help:)
– Bhushan
Aug 21 '14 at 2:43



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;







I jst Updated the answer briefly check . If still you are not getting it then please brief your doubts.
– Pravin Shinde
Aug 12 at 14:10





much better, merci!
– Pierre.Vriens
Aug 12 at 14:51






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS