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.
}
Related
I am doing the following in my laravel app,
$unique_id = uniqid();
return redirect($response['_links']['next_url']['href'])->withCookie(cookie($unique_id, $response['payment_id'], 20));
Once the redirect has been done, how I would get the value of the cookie, as when I do this,
$debug = [
'uuid' => $request->query('uuid'),
'payment_id' => $request->cookie($request->query('uuid'))
];
dd($debug);
The uuid is the $unique_id that get passed into to the redirection url, so I assumed access it like above would work but no, how can I access the value of the cookie?
A cookie with uuid is getting created and has a value,
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.
I am facing the problem with Yii 2 session when I add the products to the cart session and fetch session cart values.
session_start();
print_r($_SESSION);
exit;
I got this line.
Array ( [__flash] => Array ( ) [__id] => 65 )
Also while trying Yii 2 way:
$session = Yii::$app->session;
print_r($session);
exit;
I am getting this value:
yii\web\Session Object (
[flashParam] => __flash
[handler] => [_cookieParams:yii\web\Session:private] => Array ( [httponly] => 1 )
[_hasSessionId:yii\web\Session:private] => 1
[_events:yii\base\Component:private] => Array ( )
[_behaviors:yii\base\Component:private] =>
How to get the session data with keys and values in Yii 2?
Hi Sai you can set or retrieve the session value in yii2 easily by using the following steps
1) To set session value on var 'userVariable'
Yii::$app->session->set('userVariable','1234');
2) For getting the session value of var 'userVariable'
$userVariable = Yii::$app->session->get('userVariable');
you can get session by using $session = Yii::$app->session; hope it will help you :)
First, you need to open session
Yii::$app->session->open();
And you can get all session using $_SESSION
var_dump($_SESSION);exit;
May be useful!
You don't need to start session IF you are using YII2 framework.
Follow these step:
1. $session = Yii::$app->session;
2. $session->set('key', 'value');
3. $session->get('key');
Otherwise directly set value
$session['key']=>'value'
you can get session ID with
Yii::$app->user->id
//OR
Yii::$app->user->identity->id
and you can set new session with
$session = Yii::$app->session;
$session->set('new-name-session', '1234');
check all session with
var_dump($_SESSION);exit;
You need to process Yii::$app->session object in for/foreach loop, like so:
foreach (Yii::$app->session as $key => $value) {
var_dump($value);
}
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.
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)) : ?>