So I have stored my session data into the session like below:
$user_data = array(
'user_id' => $user_id,
'email' => $email,
'logged_in' => true
);
$this->session->set_userdata('login_session', $user_data);
In my view, I'm trying to echo the email out onto the page but no matter which way I try to access it, it will not echo out:
echo $this->session->userdata('email');
How can I access the array values?
You are storing the values in an array & then assigning that array to a variable called login_session but while retrieving the data you are just accessing with only key and not with array['key'].
So the correct syntax is:
echo $this->session->userdata('login_session')['email'];
OR
$log_sess=$this->session->userdata('login_session');
echo $log_sess['email'];
Please go through the codeigniter document as well.
Related
I recently added a column called user_cat to a table. I am not able to get this date to display on the webpage. However, I am able to display the data from columns user_id, email_id on the webpage using session variables using below.
$this->session->set_userdata("user_id", $this->session->userdata('loginUser') );
$this->session->set_userdata("user_id", $this->session->userdata('loginEmail') );
I checked the models page to and found the code to be like this.
$this->db->select('user_id,email_id');
&
'loginUser' => $row->user_id,
'loginEmail' => $row->email_id,
which i then changed to:
$this->db->select('user_id,email_id,user_cat');
&
'loginUser' => $row->user_id,
'loginCat' => $row-> user_cat,
'loginEmail' => $row->email_id,
Now i used the session variable like this:
$this->session->set_userdata("user_id", $this->session->userdata('loginCat') );
This did not work. I need help.
I assume that you didn't write the code yourself.Data from the database already set on the sessions. You can directly retrieve data using
this->session->userdata('loginUser')
So look for code like following in the model
$this->session->loginUser = //something;
//add your code
$this->session->loginCat = //similar to above
Take a look at Database reference.
However , following is the answer to your question
You are using same key to all session data. You should use a unique key for each session variable.
$this->session->set_userdata("user_id", $this->session->userdata('loginUser') );
$this->session->set_userdata("user_email", $this->session->userdata('loginEmail') );
$this->session->set_userdata("user_cat", $this->session->userdata('loginCat') );
Take a look at Session library.
first set you session like this
$data = array(
'loginUser' => $row -> user_id ,
'loginEmail' => $row -> email_id,
'loginCat' => "" ,
);
$this -> session -> set_userdata ( $data );
then update it
$data = array('loginCat' => $row -> user_cat );
$this -> session -> set_userdata ( $data );
In my Laravel project I am storing array in to session. I tried to delete the whole array from session using session forget method, but it is not working. I can access sessions after session forget. Any help would be appreciated thank you.
Storing data in to session
$item = [
'name' => 'Test',
'normalIdSession' => 'Normal Data',
'guestsSession' => '1',
'amountSession' => '200'
];
$request->session()->put('item', $item);
Deleting session
if(session()->has('item'))
{
$name = session()->get('item.name');
$normalIdSession = session()->get('item.normalIdSession');
$guestsSession = session()->get('item.guestsSession');
$amountSession = session()->get('item.amountSession');
// Here data storing in to database and deleting sessions.
session()->forget('item');
dd($name); // Here I can access session data.
}
I have stored the session varible in one controller and getting the value in another controller but the value is not passing
here is one controller
function control1 {
$this->session->set_userdata(array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
);
echo $this->session->userdata('value1'); //it returns value
}
function control2 {
echo $this->session->userdata('value1'); //it returns empty value
}
What may be the reason for this
You should check if the session has been set in the second controller like this:
function control2 {
if (!isset ($this->session->userdata('value1'))){
redirect('control1');
} else {
echo $this->session->userdata('value1'); //it returns empty value
}
If you haven't run through the control1 first, there won't be any session set yet, you see.
$this->session->set_userdata( 'values', array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
) );
$a = $this->session->userdata('values'); //it returns value
print_r( $a );
Try this code.
How the way you go into control2()? I mean, did you call control1() first to set the sessions value before goes to control2()? If so then the sessions value should be passed.
I have a multidimensional array $data on the controller side. I populate $data[$group] with any group value, between G1 - G100. I then pass the array to a view via a controller:
$this->load->view('example', $data);
On the view-side I can then access the variable, for instance $G1, $G2. The problem is that I dont know before-hand what will be passed. I can try to access my variable like this in the view:
if (isset($G1)) echo $G1;
if (isset($G2)) echo $G2;
if (isset($G3)) echo $G3;
But this becomes highly unpractical when the group variable in $data[$group] on the controller-side can have many different values.
Is there any way to check from the view beforehand what is being sent?
I don't think it is possible to know what will be passed, but you can put $data itself into an array and pass this array to the view, and in the view go through $data with a foreach:
//controller
$newdata = array(
//maybe other data
'data' => $data
);
$this->load->view('someview', $newdata);
//view
foreach($data as $key => $value){
//do whatever you like
}
Whenever i try check for a value in my userdata column of my session it thinks it's blank, when it's actually in the ci_sessions table serialised.
here's the db content unserialised:
array (
'user_data' => '',
'edit' =>
array (
'image_id' => 'HF',
'session_id' => '783c15b057bcd9c19d3fd82f367ee55d',
),
)
here's how im checking for the userdata in my view (note sessions are autoloaded)
<?php if ($this->session->userdata('edit')) : ?>
enter code here
and here's how i set the session userdata:
$values = array(
'image_id' => implode(",",$uploaded_image_ids),
'session_id' => $this->session->userdata('session_id')
);
$this->session->set_userdata('edit', $values);
Can anyone see the problem? (whole controller here http://pastebin.com/aXeRn1VN)
Also heres my config.php http://pastebin.com/A31nrC1b
View variables are separate from other variables in CI. I think (am still a bit of a CI newbie) that $this->session is not available in Views. This CI forum post discusses a couple of possible solutions to the problem http://codeigniter.com/forums/viewthread/100587/#508098 - one is to pass the value of 'userdata' from the session into the $data array you pass to the view - which seems the best solution.
You can use like this
<?php $edit = $this->session->userdata('edit'): if (!empty($edit)) : ?>