Codeigniter says session userdata blank, but exists in db - php

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

Related

Why am I not able to retrieve data - Codeigniter?

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

Laravel - Array in session not destroying using session forget method

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

Codeigniter: Accessing session data from array

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.

multiple value insert using saveField function or other function in cakephp

I insert multiple values into my database using saveField function. Here is my code.
$this->Fuel->saveField(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
);
It's not a form value. Value automatically insert when the page is refreshed.
It's only inserts first value 'device_id', $int_fuel_func[0],. Other two values are not insert into my database. Any one suggest me how to insert these values.
Thanks in advance
Cake version 2.7.5
You can simply do this as well.
$data = array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
);
$this->Fuel->save($data);
Also try this blog tutorial (cakephp blog tutorial) if you haven't been there before to be more familiar with these things.
My id is not auto Increment it is defined as varchar. Time is not include there also. I got my answer. I use it and it works.
$this->Fuel->set(
array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
)
);
$this->Fuel->save();
saveField function is only for updating one field! use updateAll and pass an array to it :
$this->Fuel->updateAll(
array(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
),
array('Fuel.id' => 234) // conditions for the Fuel you want to update
);

PHP - Session array - prevent duplicate entrys

This one is really bugging me, and can not find a easy solution.
On a detail view of a product, i set the info to a session, maximum 4:
$_SESSION['recent'][] = array(
'id' => $productimgfolder,
'title' => $product['Product']['title'],
'link' => $_SERVER['REQUEST_URI'],
'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
);
$_SESSION['recent'] = array_slice($_SESSION['recent'],-4);
This part works, if i output the session:
edit image => this is wat happens if i reload the detail view
The part i'am struggling with is, when i reload a detail view, the info in the session is duplicated.
How can i prevent this from happening?
I tried it with in_array & array_unique, i'am doing something wrong
The easy solution:
if the id is unique, you can do like this:
if(!array_key_exists ($productimgfolder, $_SESSION['recent']))
{
$_SESSION['recent'][$productimgfolder] = array(
'id' => $productimgfolder,
'title' => $product['Product']['title'],
'link' => $_SERVER['REQUEST_URI'],
'image' => 'img/products/'.$productimgfolder.'/'.$product['Product']['mainpicture']
);
}
$_SESSION['recent']=array_slice($arr, -4, 4, true);
other wise you have to foreach the recent array and check for id in the loop...
A different solution may be;
array_unshift($sessionArray, $singleArrayElement);
if (count($sessionArray) > 4) {
array_pop($seassionArray);
};
You need to check isset
if(!isset($_SESSION['recent']))
{
$_SESSION['recent'] = array()
}
THEN YOU CAN CHECK IF SESSION IS EMPTY
if(empty($_SESSION['recent']))
{
//here you add your data
}
because you are using array push, on page reload it adds data unless you check if session is empty. if yes you add data to your session.

Categories