mynameis
mynameis

Reputation: 21

ZF2: getting user session_id

I need to get session_id() in ZF2? Using session_id() won't return any value. I tried Zend_Session::getId() but it didn't work, probably because I haven't included the path (which I don't know).

Can anyone help me to find a solution. Thanks in advance.

Upvotes: 1

Views: 1368

Answers (2)

Vandan Mishra
Vandan Mishra

Reputation: 1

first of all you have to use zend session container in your page that is.

   use Zend\Session\Container;

now create an object of container by following.

    $session = new Container('User');

now set value $userid or any other variable in session by following object.

   $session->offsetSet('userId', $id);

now you can get it by following code.

    $user_id=$session->offsetGet('userId');

Upvotes: 0

Tim Fountain
Tim Fountain

Reputation: 33148

session_id() should work, but the correct way would be to access it from the session manager, e.g. (from a controller):

$sessionManager = $this->getServiceLocator()->get('Zend\Session\SessionManager');
$id = $sessionManager->getId();

If neither of these are returning a value for you then there's another problem somewhere.

Upvotes: 1

Related Questions