Redirect to login page when session expires + sile

2019-04-16 03:51发布

I want to redirect to my login page when my session is expired. What happens now:

When you are for example logged in and at the dashboard, you will see you're username in the right top corner. But when you don't do anything for 2 hours or so and then refresh you get an error that it's impossible to access an attribute 'username' of NULL. This comes because you're logged out... But how can I redirect to the login page when my session expires?

I know how I can increase the expire time but I don't know how I can redirect ..

1条回答
等我变得足够好
2楼-- · 2019-04-16 04:11

check if username is set in your session

/* NATIVE PHP */
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit;
}

/* Symfony Syntax - thanks @Touki */
if (!$request->getSession()->get('username')) {
    return new RedirectResponse('login', 301);
}
查看更多
登录 后发表回答