简体   繁体   English

会话过期+ Silex时重定向到登录页面

[英]Redirect to login page when session expires + silex

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. 但是,如果您在2个小时左右不执行任何操作然后刷新,则会收到错误消息,提示您无法访问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 .. 我知道如何增加过期时间,但不知道如何重定向..

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM