CodeIgniter Forums

Full Version: Cannot retive the session
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can store the session but i cannot retrieve it , if i try to retrieve , it is just null   .please help me  here i have attached my coding

<?php

class Login extends CI_Controller{


function index(){
$data['main_content']='login_form';

$this->load->view('includes/templates',$data);
}


function validation(){

$this->load->model('membership_validation');
$result=$this->membership_validation->validation();

if($result){

$data[]=array(

'username' => $this->input->post('username'),
'log' => TRUE,
);

$this->session->set_userdata($data);


//echo $data->$username;

$name=$this->session->userdata('username');

$log=$this->session->userdata('log');

echo $log;//RESULT IS NULL
echo $h; // RESULT IS NULL




}



}


BUT I RETRIEVE ALL DATA like this it shows
like below
i can see that my session is stored
the output for  

$this->session->all_userdata(); ,
Array ( [session_id] => 2b9b1c3cadf7cd7ff49
c6b576244d2dc [ip_address] => ::1 [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0 [last_activity] => 1420380027 [user_data] => [0] => Array ( [username] => thamarai [log] => 1 ) )



$s=$this->session->userdata('session_id');

echo $s;// it is working it shows some id , but the session which i'm storing like username, log. i cant retrieve it
Code:
$data[] = array(

'username' => $this->input->post('username'),
'log' => TRUE,
);

This is the issue
you can do this:
Code:
$data['username'] =  $this->input->post('username');
$data['log'] = TRUE;

then u can access it like this :
Code:
$name = $this->session->userdata('username');
$log = $this->session->userdata('log');

or u can do this :
Code:
$data['user_info']=array(

'username' => $this->input->post('username'),
'log' => TRUE,
);
$user_info = $this->session->userdata('user_info');
$name = $user_info['username'];


and don't forget that session will be available on the next request(I mean you need to refresh page then display it,you can not display sessions right after seting it)