php 如何清除 Zend framework2 中的会话容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15195290/
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
How to clear a session container in Zend framework2
提问by aravind.udayashankara
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 ) . Is there a way to achieve this
我只想清除名为“user”的容器,它有很多键(我不知道最后会是什么)。有没有办法实现这一目标
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 .
预先感谢您回复此帖子。
回答by Crisp
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']);
回答by Mohd Belal
Following are the details for Destroying the session in Zend Framework 2:
以下是在 Zend Framework 2 中销毁会话的详细信息:
Using Basic PHP Functionality
session_start()function starts the session.session_destroy()function deletes ALLthe data stored in session array.
使用基本的 PHP 功能
session_start()函数启动会话。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 中创建一个会话,然后进行删除过程。
- Creating Session
- 创建会话
use Zend\Session\Container;
$session_container = new Container('user_session');
$session_container->last_login = date('Y-m-d H:i:s');
$session_container->sess_token = trim(base64_encode(md5(microtime())), "=");
使用 Zend\Session\Container;
$session_container = new Container('user_session');
$session_container->last_login = date('Ymd H:i:s');
$session_container->sess_token = trim(base64_encode(md5(microtime())), "=");
- Deleting Session
- 删除会话
$session = new Container("user_session");
$session->getManager()->getStorage()->clear('user_session');
$session = new Container("user_session");
$session->getManager()->getStorage()->clear('user_session');
Where user_sessionis the name of session array key for storing the details.
其中user_session是用于存储详细信息的会话数组键的名称。
回答by aravind.udayashankara
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 方法的人。
回答by user2644574
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.
我希望这有帮助。
回答by Mr Coder
Container::getDefaultManager()->getStorage()->clear('user');
回答by Agustincl
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();
回答by Lucas Tourinho Cavalcante
You can clear session like this:
您可以像这样清除会话:
$this->session->exchangeArray(array());
$this->session->exchangeArray(array());

