b174008 b174008 - 2 months ago 17
PHP Question

Symfony2 - redirect user back after re-login, if logged out because session expired?

In Symfony 2.8, if user's session expires, he'll be logged out and redirected to login page after he makes new request.

After user logs in again, how can I redirect the user back to the page he wanted to open before he was redirected? Or at least to the last page he opened (page, on which session expired)?


You could let the login action check or there was a referer, and update the security target path. Make sure that referrer is safe before you do this ;-)

$yourReferer = $request->headers->get('referer');
$this->container->get('session')->set('_security.main.target_path', $yourReferer);

The login mechanism will use that target_path after a successful login attempt.