How to Delete The Session and cookie in symfony2
I had some other cookies than REMEMBERME
which should be removed after successful logout. And I found that Symfony already provides the functionality with delete_cookies
key under logout
key in security.yml
file. See the example code in the official doc for v2.8 line 240.
In summary,
security: ... firewalls: your_name_of_the_firewall: ... logout: ... delete_cookies: your_name_of_the_cookie: { path: null, domain: null }
I couldn't check for the older version, but at least it has been there since v2.7. For me, the documentation was not so clear enough, and I had to check the code. FYI, it registers CookieClearingLogoutHandler
, and there is the actual code which deletes the cookies.
Try this..
//clear Session$session->remove('user_id');$session->clear();//clear Cookiepublic Boolean invalidate(integer $lifetime = null)public Boolean migrate(Boolean $destroy = false, integer $lifetime = null)
Refer following link:
http://symfony.com/doc/current/components/http_foundation/sessions.html
http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/Session.html
The simplest solution that's worked for me is:
$response = new Response();$response->headers->clearCookie(\session_name());
Or more thoroughly:
$params = \session_get_cookie_params();$response->headers->clearCookie( \session_name(), $params['path'], $params['domain'], $params['secure'], isset($params['httponly']));