How to clear a session container in Zend framework2
Asked Answered
H

7

19

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 .

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 )

$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

I know I can do offsetunset on each key but thats not an Optimal solution I feel .

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

Thanks in advance for responding to this posting .

Hudak answered 4/3, 2013 at 5:43 Comment(0)
C
45

You almost had it, you just need to pass the namespace to the clear method

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

You can still treat the $_SESSION like an array, too, so the following also works

unset($_SESSION['user']); 
Centrepiece answered 4/3, 2013 at 6:45 Comment(1)
Wow this is the one which I didnt tried , looks correct will try an get back , but some how, doesn’t make any sense in the way framework works , $session_user is already an object of container called user again we are saying in the clear method to clear user container . I tried like this before $session_user->getManager()->getStorage()->user->clear(); and got an error let me check and get back to youHudak
H
1

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

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 .

This may help others who are possessive in creating objects of each session container and call clear method .

Hudak answered 4/3, 2013 at 8:21 Comment(0)
A
0

To destroy all the sessions:

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

  or

use the simple php destroy function:

 session_destroy();

This function clears all the sessions.

I hope this helps.

Aflcio answered 6/8, 2013 at 10:13 Comment(0)
N
0

Following are the details for Destroying the session in Zend Framework 2:

  • Using Basic PHP Functionality

    session_start() function starts the session.

    session_destroy() function deletes ALL the data stored in session array.

Now using Zend Framework functionality:

For clear understanding lets first, create a session in Zend Framework and then make a Delete process.

  1. 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())), "=");

  1. Deleting Session

$session = new Container("user_session");

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

Where user_session is the name of session array key for storing the details.

Neckar answered 22/12, 2017 at 10:5 Comment(0)
E
0
 Container::getDefaultManager()->getStorage()->clear('user');
Eraser answered 21/11, 2019 at 11:17 Comment(0)
F
-1

In ZF2, for Container use;

Create container:

$sessionTokenizer = new Container('token');

Set variable into container

$token = $sessionTokenizer->token;

Destroy container (ONLY CONTAINER)

$sessionTokenizer->offsetUnset();
Fourway answered 30/3, 2015 at 19:55 Comment(1)
Method offsetUnset has mandatory parameter $key so this should not be workingSlop
B
-2

You can clear session like this:

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

Beryl answered 1/3, 2014 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.