php Yii 会话管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13541565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:39:07  来源:igfitidea点击:

Yii session management

phpsessionyii

提问by Varun Jain

I am trying to set some session variable in Yii using the following code :

我正在尝试使用以下代码在 Yii 中设置一些会话变量:

    $session=new CHttpSession;
            $session->open() ;
            $session->setSessionName('My Session') ; 
            $session['sleep'] = 0 ;
            $session['attempts'] = 0 ;
            $session->writeSession('sleep','0') ; 
            $session['ip'] = $this->get_ip_address() ; $session->close() ;    var_dump($session,$session['ip']) ; 

However , I am not able to set the session vaiables above , the dump has the following result :

但是,我无法设置上面的会话变量,转储结果如下:

object(CHttpSession)#17 (5) { ["autoStart"]=> bool(true) ["behaviors"]=> array(0) { } ["_initialized":"CApplicationComponent":private]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } string(3) "::1" 

It sees $session and $session['ip'] as two completely different variables . Can someone help me out with this ?

它将 $session 和 $session['ip'] 视为两个完全不同的变量。有人可以帮我解决这个问题吗?

回答by Skatox

I work with sessions under Yii in another way, I use the global session variable under the app() variable. So you can store values like this:

我以另一种方式处理 Yii 下的会话,我使用 app() 变量下的全局会话变量。所以你可以存储这样的值:

Yii::app()->session['sleep'] = "value";

And you can get values like this:

你可以得到这样的值:

$sleep = Yii::app()->session['sleep'];

Finally you can remove it like this:

最后你可以像这样删除它:

unset(Yii::app()->session['sleep']);

This way you can access them everywhere in your code. I recommend you to read this article: http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/it has all the information that you need.

这样您就可以在代码中的任何地方访问它们。我建议您阅读这篇文章:http: //www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/它包含您需要的所有信息。

回答by Akhil K R

To remove the session variable in yii...

要删除 yii 中的会话变量...

Please use this actual format.

请使用此实际格式。

Yii::app()->session->remove('session_name');

http://www.yiiframework.com/doc/api/1.1/CHttpSession#remove-detail

http://www.yiiframework.com/doc/api/1.1/CHttpSession#remove-detail

回答by Jitendra Y

$session = new \yii\web\Session();

$session->open(); 

$session['account_id'] = $id; 

$session['account_name'] = $name;