如何在 Symfony 中检测会话超时

发布于 2024-08-13 15:37:27 字数 202 浏览 2 评论 0原文

我目前正在开发一个使用 PHP 框架 Symfony 的项目。 我配置了安全页面,定义了 30 分钟的会话超时,并设置了如果未经身份验证的用户尝试访问安全页面时应重定向用户的页面。

我已经说过,如果用户在用户会话过期后尝试访问安全页面,用户也会被重定向到那里。 在这种情况下,我想显示一条消息,基本上是“您的会话已过期。请重新连接。”

我怎样才能做到这一点?

I'm currently working on a project with the PHP framework Symfony.
I've configured secured pages, defined 30 minutes for the session timeout and set the page where the user should be redirect if an unauthenticated user tries to access a secured page.

I've remarked that the user is also redirected there if the user tries to access a secured page after the user's session has expired.
In this case, I would like to display a message basically saying that "Your session has expired. Please reconnect."

How could I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-08-20 15:37:27

您可以在每次成功的身份验证时设置一个 cookie。然后,如果会话为空,但 cookie 存在,则您会说“会话已过期”。

另一种更丑陋的方法,即使在禁用 cookie 的情况下也能工作:始终将会话 ID 添加到 URL。然后,如果它出现在 URL 中,但会话为空,则说明我们处于过期状态。

You can set a cookie on each successful authentication. Then, if the session is empty, but the cookie is present, you say "Session expired".

Another, uglier way, which will work even with cookies disabled: always add the session ID to the URL. Then, again, if it's present in the URL, but the session is empty, we're in the expiration situation.

揽清风入怀 2024-08-20 15:37:27

检查 myUser->timedout 更容易。

http://trac.symfony-project.org/changeset/1722

Symfony 1.4 没有有这个方法,但是你可以创建它:

class myUser extends sfBasicSecurityUser
{
   // ...   
   public function getTimedOut() 
   {
       return $this->timedout;
   }
   // ...
}

正确填写属性timedout。

然后在模板中检查它的值:

<?php if($sf_user->getTimedOut()) : ?>
    <div class="error">Session timeout...</div>
<?php endif ?>   

Much easier is checking for myUser->timedout.

http://trac.symfony-project.org/changeset/1722

Symfony 1.4 does not have this method, but you can create it:

class myUser extends sfBasicSecurityUser
{
   // ...   
   public function getTimedOut() 
   {
       return $this->timedout;
   }
   // ...
}

The attribute timedout is filled correctly.

Then check for its value in your template:

<?php if($sf_user->getTimedOut()) : ?>
    <div class="error">Session timeout...</div>
<?php endif ?>   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文