Question

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 ..

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top