Reputation: 93
I want to destroy a session in one of my Symfony2 controllers and am using following code:
$this->get('session')->invalidate();
But it appears that this does not in fact get rid of the session data and I have to manually remove session variables like so:
$session->remove('viewed');
$session->remove('name');
Is there a way I can troubleshoot why invalidate() does not clear the session?
P.S. If I do:
session_start();
session_destroy();
It works, so it's not a server/php/browser issue.
Upvotes: 5
Views: 8544
Reputation: 381
It does work as follows:
$request->getSession()->invalidate(1);
As invalidate leaves the current session untouched if you are not providing any parameter you have to set the lifetime on 1 (one second)
http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/Session.html#method_invalidate
Upvotes: 6
Reputation: 4304
If you're using a custom session name start the session with that name first before invalidating it to successfully clear it.
$session = $this->get('session');
$session->setName('session_name');
$session->start();
$session->invalidate();
Upvotes: 1
Reputation: 71
Make sure that you have a session to invalidate in your script.
$session->start(); // Start the session...
$session->invalidate(); // ...then invalidate
Upvotes: 7
Reputation: 2576
Try:
$this->get('session')->clear();
If that doesn't work you could use this:
$session = $this->get('session');
$ses_vars = $session->all();
foreach ($ses_vars as $key => $value) {
$session->remove($key);
}
Upvotes: 4