简体   繁体   English

如何清除 Zend framework2 中的会话容器

[英]How to clear a session container in Zend framework2

I have recently started building an application using Zendframework 2 , I have good experience in ZF1 , the major problem I am facing here with ZF2 is with sessions .我最近开始使用 Zendframework 2 构建应用程序,我在 ZF1 方面有很好的经验,我在 ZF2 中面临的主要问题是会话。

Here is the way that I am creating a session container .这是我创建会话容器的方式。

use Zend\Session\Container;

// Session container creation: ( previously we were calling it as namespaces ) // 会话容器创建:(之前我们将其称为命名空间)

$session_user = new Container('user');
$session_user_errors = new Container('usererrors');
$session_user_shares = new Container('usershares');

Now Like this I have several containers ,现在像这样我有几个容器,

I could clear a key of a particular container like this我可以像这样清除特定容器的密钥

// Getting value from the session by key: ( get value from namespace ) // 通过 key 从会话中获取值:(从命名空间获取值)

$email = $session_user->offsetGet('email');

// Setting value in session: ( set value from namespace ) // 在会话中设置值:(从命名空间设置值)

$session_user->offsetSet('username', 'abcd');

Now my problem is to clear an entire container which are set in several levels of my application .现在我的问题是清除在我的应用程序的几个级别中设置的整个容器。

If I try the below code Its clearing all of my session containers .如果我尝试下面的代码,它会清除我所有的会话容器。

$session_user = new Container('user');
$session_user->getManager()->getStorage()->clear();

I want to clear only the container called 'user' which has many keys ( I dont know what all will be there at end ) .我只想清除名为“user”的容器,它有很多键(我不知道最后会是什么)。 Is there a way to achieve this有没有办法实现这一目标

I know I can do offsetunset on each key but thats not an Optimal solution I feel .我知道我可以对每个键进行 offsetunset ,但这不是我觉得的最佳解决方案。

Please kindly suggest if any alternative way is there to clear a particular session container .请建议是否有任何替代方法来清除特定的会话容器。

NOTE : - I am not using any of the third party modules like ZfcUser and Akrabat sessions注意: -我没有使用任何第三方模块,如 ZfcUser 和 Akrabat 会话

Thanks in advance for responding to this posting .预先感谢您回复此帖子。

You almost had it, you just need to pass the namespace to the clear method你几乎拥有它,你只需要将命名空间传递给 clear 方法

$session_user->getManager()->getStorage()->clear('user');

You can still treat the $_SESSION like an array, too, so the following also works您仍然可以将 $_SESSION 视为数组,因此以下内容也有效

unset($_SESSION['user']); 

Following are the details for Destroying the session in Zend Framework 2:以下是在 Zend Framework 2 中销毁会话的详细信息:

  • Using Basic PHP Functionality使用基本的 PHP 功能

    session_start() function starts the session. session_start()函数启动会话。

    session_destroy() function deletes ALL the data stored in session array. session_destroy()函数删除存储在会话数组中的所有数据。

Now using Zend Framework functionality:现在使用 Zend 框架功能:

For clear understanding lets first, create a session in Zend Framework and then make a Delete process.为了清楚地理解,让我们首先在 Zend Framework 中创建一个会话,然后进行删除过程。

  1. Creating Session创建会话

use Zend\\Session\\Container;使用 Zend\\Session\\Container;

$session_container = new Container('user_session'); $session_container = new Container('user_session');

$session_container->last_login = date('Ymd H:i:s'); $session_container->last_login = date('Ymd H:i:s');

$session_container->sess_token = trim(base64_encode(md5(microtime())), "="); $session_container->sess_token = trim(base64_encode(md5(microtime())), "=");

  1. Deleting Session删除会话

$session = new Container("user_session"); $session = new Container("user_session");

$session->getManager()->getStorage()->clear('user_session'); $session->getManager()->getStorage()->clear('user_session');

Where user_session is the name of session array key for storing the details.其中user_session是用于存储详细信息的会话数组键的名称。

The Solution posted By @Crisp worked like a Charm But here is the alternative way what I found after a research to solve this problem @Crisp 发布的解决方案就像一个魅力但这是我在研究后发现的替代方法来解决这个问题

use Zend\Session\SessionManager;

$sessionManager = new SessionManager();

//get array of sessions from storage 
$array_of_sessions = $sessionManager->getStorage();

//Unset which ever container you want by passing its name ( ZF1 its called namespace ) 
 unset($array_of_sessions['user']);
 unset($array_of_sessions['usershares']);
 unset($array_of_sessions['actions']);

I feel session manager is the one that we need to use to manage sessions whether to clear or read and container is one of the entity which is managed by session manager .我觉得 session manager 是我们需要用来管理 session 的一个,无论是清除还是读取,容器是 session manager 管理的实体之一。

This may help others who are possessive in creating objects of each session container and call clear method .这可能有助于其他拥有创建每个会话容器的对象并调用 clear 方法的人。

To destroy all the sessions:销毁所有会话:

  $session = new Container('base');
  $session->getManager()->destroy();

  or

use the simple php destroy function:使用简单的 php destroy 函数:

 session_destroy();

This function clears all the sessions.此功能清除所有会话。

I hope this helps.我希望这有帮助。

 Container::getDefaultManager()->getStorage()->clear('user');

In ZF2, for Container use;在 ZF2 中,供 Container 使用;

Create container:创建容器:

$sessionTokenizer = new Container('token');

Set variable into container将变量设置到容器中

$token = $sessionTokenizer->token;

Destroy container (ONLY CONTAINER)销毁容器(仅容器)

$sessionTokenizer->offsetUnset();

You can clear session like this:您可以像这样清除会话:

$this->session->exchangeArray(array()); $this->session->exchangeArray(array());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM