0

我运行一个站点,人们可以通过不同的门户登录,这些门户并不总是在我的域中。

当有人注销时,他们会被重定向到他们的原始门户网址。

我将 php 会话超时设置为半小时,但是我遇到了一个问题,即我的一些用户在超时期间处于休眠状态。因此,当他们回到他们在我的网站上所做的事情时并重新加载他们不可避免地要带到我主页的页面。

我收到了一些关于此的投诉。

作为一种可能的解决方法,我在考虑:

1 - 针对用户将会话 ID 存储在数据库中。

2 - 当用户通过超时运行检查时,获取传递的会话 ID 以查找用户是谁,然后重定向到原始门户页面。

这是很好的逻辑吗?我认为我对会话 ID 的了解不够。超时后会话在浏览器中被终止,还是我仍然可以在服务器端获取它?

谢谢,约翰

4

2 回答 2

0

Each time session_start is called the session files timestamp (if it exists) gets updated, which is used to calculated if session.gc_maxlifetime has been exceeded.

More importantly you can't depend on a session to expire after session.gc_maxlifetime time has been exceeded.

PHP runs garbage collection on expired sessions after the current session is loaded and by using session.gc_probability and session.gc_divisor it calculates the probability that garbage collection will run. By default its a 1% probability.

If you have a low number of visitors there is a probability that an inactive user could access a session that should have expired and been deleted. If this is important to you will need to store a timestamp in the session and calculate how log a user has been inactive.

This example replaces session_start and enforces a timeout:

function my_session_start($timeout = 1440) {
    ini_set('session.gc_maxlifetime', $timeout);//change the session timeout
    session_start();

    if (isset($_SESSION['timeout_idle']) && $_SESSION['timeout_idle'] < time()) {
        session_destroy();
        session_start();
        session_regenerate_id();
        $_SESSION = array();
    }

    $_SESSION['timeout_idle'] = time() + $timeout;
}
于 2013-10-31T20:14:11.613 回答
0

我认为存储 session_id 是没用的,因为它在超时后将无效。一旦会话到期,就是这样。一旦你调用session_start(),就会发生垃圾收集。

一些选项:

  • 如果 30 分钟内没有任何活动,则警告用户,然后出于“安全”目的清除当前会话,然后忘记投诉,因为它成为用户的责任。
  • cron 工作,如果你不介意你的服务器做一些额外的工作,这很容易根据$_SESSION数据大小、会话数和运行频率来破坏它。
  • 带有setInterval()要刷新的 JS(或需要时重定向)。
  • 元刷新(如果需要,使用重定向)。
  • 为站点的每个用户|部分设置不同的会话超时值。这是一个解释如何的帖子,但我从未尝试过。
于 2013-10-31T10:42:53.710 回答