如何在 Yii 2 中检索会话值 [英] How to retrieve the session values in Yii 2

查看:26
本文介绍了如何在 Yii 2 中检索会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将产品添加到购物车会话并获取会话购物车值时,我遇到了 Yii 2 会话的问题.

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;

我得到了这条线.

Array ( [__flash] => Array ( ) [__id] => 65 )

也在尝试 Yii 2 的时候:

Also while trying Yii 2 way:

$session = Yii::$app->session;
print_r($session);
exit;

我得到这个值:

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] =>

如何在 Yii 2 中获取带有键和值的会话数据?

How to get the session data with keys and values in Yii 2?

推荐答案

你可以使用以下步骤轻松设置或检索 yii2 中的会话值

Hi Sai you can set or retrieve the session value in yii2 easily by using the following steps

1) 在 var 'userVariable' 上设置会话值

1) To set session value on var 'userVariable'

Yii::$app->session->set('userVariable','1234');

2) 用于获取 var 'userVariable' 的会话值

2) For getting the session value of var 'userVariable'

$userVariable = Yii::$app->session->get('userVariable');

这篇关于如何在 Yii 2 中检索会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆