会话过期时重定向到登录页面 + silex [英] Redirect to login page when session expires + silex

查看:73
本文介绍了会话过期时重定向到登录页面 + silex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的会话过期时,我想重定向到我的登录页面.现在发生了什么:

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

例如,当您登录并访问仪表板时,您会在右上角看到您的用户名.但是,当您在 2 小时左右不做任何事情然后刷新时,您会收到一个错误,即无法访问 NULL 的属性用户名".这是因为您已注销...但是当我的会话过期时,我如何重定向到登录页面?

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

推荐答案

检查您的会话中是否设置了用户名

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

这篇关于会话过期时重定向到登录页面 + silex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆