php Yii 中的会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20965023/
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 03:42:24  来源:igfitidea点击:

Sessions in Yii

phpsessionyii

提问by Tested

Here I go what am doing is I am using

在这里,我正在做的是我正在使用

 Yii::app()->SESSION['userid']

with no

没有

  Yii::app()->session->open();

at login

登录时

  Yii::app()->session->destroy();

at logout

登出时

I wanna know if dont do the open and destroy session is it worthy . Does Yii do it internally.

我想知道如果不进行打开和销毁会话是否值得。Yii 是否在内部进行。

One more strange thing I dont know whats happening. In the same browser for a session I can login for multiple users .. this should not happen so.Is it that i am not using the open and destroy session methods .

还有一件奇怪的事情我不知道发生了什么。在会话的同一个浏览器中,我可以为多个用户登录.. 这不应该发生。是不是我没有使用打开和销毁会话方法。

 public function actionLogout()
{
    Yii::app()->user->logout();
    Yii::app()->session->clear();
    $this->redirect(Yii::app()->controller->module->returnLogoutUrl);
}

Please let me know how do i figure this out

请让我知道我如何解决这个问题

回答by Moyed Ansari

For creating yii session

用于创建 yii 会话

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

You can get value like this

你可以得到这样的价值

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

And unset session like

和未设置的会话

unset(Yii::app()->session['userid']); # Remove the session

In case of user signs out , you have to remove all the session.

如果用户退出,您必须删除所有会话。

Yii::app()->session->clear();

After this, you need to remove actual data from server

在此之后,您需要从服务器中删除实际数据

Yii::app()->session->destroy();

回答by Zombyii

Don't clear session, only logout:

不清除会话,只注销:

Yii::app()->user->logout(false);

回答by Vinod Tigadi

In YII, the session is handled by 'CHttpSession' class - http://www.yiiframework.com/doc/api/1.1/CHttpSession

在 YII 中,会话由“CHttpSession”类处理 - http://www.yiiframework.com/doc/api/1.1/CHttpSession

Should you use the method 'open()' Yii::app()->session->open();depends on your configuration. If in the main.php, you have set the
'session' => array ( 'autoStart' => true, ),then the Session will be started automatically by YII itself. You can refer the source code for the method 'init()' here - https://github.com/yiisoft/yii/blob/1.1.16/framework/web/CHttpSession.php#L83

您是否应该使用“open()”方法Yii::app()->session->open();取决于您的配置。如果在 中main.php,您设置了
'session' => array ( 'autoStart' => true, ),Session 将由 YII 本身自动启动。您可以在此处参考“init()”方法的源代码 - https://github.com/yiisoft/yii/blob/1.1.16/framework/web/CHttpSession.php#L83

Regarding your question about using the methods 'close()' or 'destroy()', the method 'close()' only unsets the keys of Session but 'destroy' removes the whole session data

关于使用方法“close()”或“destroy()”的问题,方法“close()”仅取消设置会话的键,但“销毁”删除整个会话数据

回答by Vinod Tigadi

Once you craeted session it will allow you in same browser multiple time, i mean for same url it will allow you to login, you can just do it rename your session variable with different name and check that particuller variable to login with that.

一旦你创建了会话,它就会允许你在同一个浏览器中多次使用,我的意思是对于同一个 url,它允许你登录,你可以用不同的名称重命名你的会话变量,并检查该 particuller 变量以登录。

Session is a Web application component that can be accessed via Yii::$app->session.

Session 是一个 Web 应用组件,可以通过 Yii::$app->session 访问。

To start the session, call open(); To complete and send out session data, call close(); To destroy the session, call destroy().

要开始会话,请调用 open(); 要完成并发送会话数据,请调用 close(); 要销毁会话,请调用 destroy()。

Session can be used like an array to set and get session data. For example,

Session 可以像数组一样使用来设置和获取会话数据。例如,

$session = new Session;

$session->open();

$value1 = $session['name1'];  // get session variable 'name1'

$value2 = $session['name2'];  // get session variable 'name2'

foreach ($session as $name => $value) // traverse all session variables

$session['name3'] = $value3;  // set session variable 'name3'