【问题标题】:Sessions expire on single page website会话在单页网站上过期
【发布时间】:2014-08-21 06:56:09
【问题描述】:

我创建了一个用户需要登录的单页网站。用户在此页面上停留的时间可能不同,他们必须选择可能需要 15 分钟到几个小时的产品。

选择完成后,选择通过电子邮件发送,并使用 AJAX 调用发送电子邮件。在此 AJAX 调用中,$_SESSION['user_id'] 用于识别用户,因此我们知道选择来自谁。

如果用户花费的时间超过 1440 秒(24 分钟),则会话过期。现在我将会话超时更改为 3 小时,但问题仍然存在。我几乎无法想象有人需要超过 3 个小时。但他们可能会。也许用户登录,买了一些杂货然后继续。

那么我该如何解决这个问题呢?我想每 5 分钟使用一次 AJAX 调用,希望会话超时时间会被重置。但我不知道这是否可行,以及是否可以解决此问题。

【问题讨论】:

  • 你有没有像session_start();这样在页面顶部开始会话
  • @SKRocks,是的,我在用户登录后开始了会话。登录页面是一个不同的页面,用户在登录后被重定向到另一个页面。该页面执行session_start()
  • 所有页面?您在哪里设置以及在哪里访问会话?
  • 通过定期向服务器发送一些请求来保持会话处于活动状态。

标签: php ajax session


【解决方案1】:
NOTE: I have edited the answer. Check it out fully. 
      First time I missed some points

我有办法..

虽然有点棘手。

如果用户在页面内,如果他是空闲的,那么他的鼠标就不会移动..

你可以做的事情是在鼠标移动时你可以调用一个ajax函数,它将鼠标移动的时间设置到与用户相对应的数据库中。

说用户表是这样的

|-----------------------------------------------|
|   id   |    last_active_time  |  login_status |
|-----------------------------------------------|
|    3   |        1243236456    |      1        |
|-----------------------------------------------|

其中 last_activity_contains 由 time() 提供的 php 时间戳;并且 login_status = 1 of user is logged in and login_status = 0 if user is not login.

When a user logs in and his session starts, 
          change the login_status = 1,
When user manualy logs out change login_status = 0. 
So from backend side, you can also check whether a user is 
logged in w.r.t the value of login_side

现在每次鼠标移动时,该值都会更新。

现在你必须从服务器运行一个 cron j0b,它会调用一个 php 文件,该文件反过来会检查 mousemove 时间和当前时间之间的差异是否超过 30 分钟..

如果超过 30 分钟,则将 login_status 设置为 0。

EDIT:

You can change the 30 minutes frame to 1 or 2 hour.
The other solution is put another ajax call in every 10 minutes, which will check if
the login_status is 0 or 1. If the login status is 1 then the session will again be 
refreshed and as such the time_out will again start for next timeout session,

【讨论】:

  • 我明白你写的。但这将注销用户,我不希望用户注销。会话必须保持存在,即使在 1 小时不活动后也是如此。
猜你喜欢
  • 2012-11-02
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多