Question

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 .

Was it helpful?

Solution

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']); 

OTHER TIPS

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.

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 .

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.

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

In ZF2, for Container use;

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());

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top